2004-10-07 12:16:33 +08:00
|
|
|
//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-10-07 12:16:33 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-10-07 12:16:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass transforms simple global variables that never have their address
|
|
|
|
// taken. If obviously true, it marks read/write globals as constant, deletes
|
|
|
|
// variables only stored to, etc.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2014-06-28 02:19:56 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2014-03-04 19:01:28 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2015-11-15 22:21:37 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2014-03-04 18:40:04 +08:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2014-03-04 19:17:44 +08:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2004-10-07 12:16:33 +08:00
|
|
|
#include "llvm/Pass.h"
|
2007-01-31 07:46:24 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-04-26 15:40:11 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-05-03 02:35:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/CtorUtils.h"
|
2013-10-22 01:14:55 +08:00
|
|
|
#include "llvm/Transforms/Utils/GlobalStatus.h"
|
2013-07-25 11:23:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2004-10-07 12:16:33 +08:00
|
|
|
#include <algorithm>
|
2014-05-03 23:50:37 +08:00
|
|
|
#include <deque>
|
2004-10-07 12:16:33 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "globalopt"
|
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumMarked , "Number of globals marked constant");
|
2011-01-20 00:32:21 +08:00
|
|
|
STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
|
|
|
|
STATISTIC(NumHeapSRA , "Number of heap objects SRA'd");
|
|
|
|
STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
|
|
|
|
STATISTIC(NumDeleted , "Number of globals deleted");
|
|
|
|
STATISTIC(NumGlobUses , "Number of global uses devirtualized");
|
2013-10-08 03:03:24 +08:00
|
|
|
STATISTIC(NumLocalized , "Number of globals localized");
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
|
|
|
|
STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");
|
|
|
|
STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
|
2008-02-17 04:56:04 +08:00
|
|
|
STATISTIC(NumNestRemoved , "Number of nest attributes removed");
|
2009-02-15 17:56:08 +08:00
|
|
|
STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
|
|
|
|
STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
|
2011-03-21 01:59:11 +08:00
|
|
|
STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
|
2004-10-07 12:16:33 +08:00
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
struct GlobalOpt : public ModulePass {
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2015-01-15 18:41:28 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2015-11-15 22:21:37 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
}
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
GlobalOpt() : ModulePass(ID) {
|
|
|
|
initializeGlobalOptPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
|
|
|
|
private:
|
2005-09-26 09:43:45 +08:00
|
|
|
bool OptimizeFunctions(Module &M);
|
|
|
|
bool OptimizeGlobalVars(Module &M);
|
2009-03-06 18:21:56 +08:00
|
|
|
bool OptimizeGlobalAliases(Module &M);
|
2015-12-23 03:38:07 +08:00
|
|
|
bool deleteIfDead(GlobalValue &GV);
|
2015-12-23 04:43:30 +08:00
|
|
|
bool processGlobal(GlobalValue &GV);
|
2015-12-23 03:16:50 +08:00
|
|
|
bool processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS);
|
2011-03-21 01:59:11 +08:00
|
|
|
bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
|
2015-11-16 18:16:22 +08:00
|
|
|
|
|
|
|
bool isPointerValueDeadOnEntryToFunction(const Function *F,
|
|
|
|
GlobalValue *GV);
|
|
|
|
|
2012-02-12 09:13:18 +08:00
|
|
|
TargetLibraryInfo *TLI;
|
2014-10-08 15:23:31 +08:00
|
|
|
SmallSet<const Comdat *, 8> NotDiscardableComdats;
|
2004-10-07 12:16:33 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2004-10-07 12:16:33 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char GlobalOpt::ID = 0;
|
2011-12-02 05:29:16 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt",
|
|
|
|
"Global Variable Optimizer", false, false)
|
2015-01-15 18:41:28 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
2015-11-15 22:21:37 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2011-12-02 05:29:16 +08:00
|
|
|
INITIALIZE_PASS_END(GlobalOpt, "globalopt",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Global Variable Optimizer", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2004-10-07 12:16:33 +08:00
|
|
|
ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Is this global variable possibly used by a leak checker as a root? If so,
|
|
|
|
/// we might not really want to eliminate the stores to it.
|
2012-07-24 15:21:08 +08:00
|
|
|
static bool isLeakCheckerRoot(GlobalVariable *GV) {
|
|
|
|
// A global variable is a root if it is a pointer, or could plausibly contain
|
|
|
|
// a pointer. There are two challenges; one is that we could have a struct
|
|
|
|
// the has an inner member which is a pointer. We recurse through the type to
|
|
|
|
// detect these (up to a point). The other is that we may actually be a union
|
|
|
|
// of a pointer and another type, and so our LLVM type is an integer which
|
|
|
|
// gets converted into a pointer, or our type is an [i8 x #] with a pointer
|
|
|
|
// potentially contained here.
|
|
|
|
|
|
|
|
if (GV->hasPrivateLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<Type *, 4> Types;
|
2016-01-17 04:30:46 +08:00
|
|
|
Types.push_back(GV->getValueType());
|
2012-07-24 15:21:08 +08:00
|
|
|
|
|
|
|
unsigned Limit = 20;
|
|
|
|
do {
|
|
|
|
Type *Ty = Types.pop_back_val();
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
default: break;
|
|
|
|
case Type::PointerTyID: return true;
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
case Type::VectorTyID: {
|
|
|
|
SequentialType *STy = cast<SequentialType>(Ty);
|
|
|
|
Types.push_back(STy->getElementType());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::StructTyID: {
|
|
|
|
StructType *STy = cast<StructType>(Ty);
|
|
|
|
if (STy->isOpaque()) return true;
|
|
|
|
for (StructType::element_iterator I = STy->element_begin(),
|
|
|
|
E = STy->element_end(); I != E; ++I) {
|
|
|
|
Type *InnerTy = *I;
|
|
|
|
if (isa<PointerType>(InnerTy)) return true;
|
|
|
|
if (isa<CompositeType>(InnerTy))
|
|
|
|
Types.push_back(InnerTy);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (--Limit == 0) return true;
|
|
|
|
} while (!Types.empty());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a value that is stored to a global but never read, determine whether
|
|
|
|
/// it's safe to remove the store and the chain of computation that feeds the
|
|
|
|
/// store.
|
2012-08-29 23:32:21 +08:00
|
|
|
static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI) {
|
2012-07-24 15:21:08 +08:00
|
|
|
do {
|
|
|
|
if (isa<Constant>(V))
|
|
|
|
return true;
|
|
|
|
if (!V->hasOneUse())
|
|
|
|
return false;
|
2012-07-26 05:19:40 +08:00
|
|
|
if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
|
|
|
|
isa<GlobalValue>(V))
|
2012-07-24 15:21:08 +08:00
|
|
|
return false;
|
2012-08-29 23:32:21 +08:00
|
|
|
if (isAllocationFn(V, TLI))
|
2012-07-24 15:21:08 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
Instruction *I = cast<Instruction>(V);
|
|
|
|
if (I->mayHaveSideEffects())
|
|
|
|
return false;
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
if (!GEP->hasAllConstantIndices())
|
|
|
|
return false;
|
|
|
|
} else if (I->getNumOperands() != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
V = I->getOperand(0);
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// This GV is a pointer root. Loop over all users of the global and clean up
|
|
|
|
/// any that obviously don't assign the global a value that isn't dynamically
|
|
|
|
/// allocated.
|
2012-08-29 23:32:21 +08:00
|
|
|
static bool CleanupPointerRootUsers(GlobalVariable *GV,
|
|
|
|
const TargetLibraryInfo *TLI) {
|
2012-07-24 15:21:08 +08:00
|
|
|
// A brief explanation of leak checkers. The goal is to find bugs where
|
|
|
|
// pointers are forgotten, causing an accumulating growth in memory
|
|
|
|
// usage over time. The common strategy for leak checkers is to whitelist the
|
|
|
|
// memory pointed to by globals at exit. This is popular because it also
|
|
|
|
// solves another problem where the main thread of a C++ program may shut down
|
|
|
|
// before other threads that are still expecting to use those globals. To
|
|
|
|
// handle that case, we expect the program may create a singleton and never
|
|
|
|
// destroy it.
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// If Dead[n].first is the only use of a malloc result, we can delete its
|
|
|
|
// chain of computation and the store to the global in Dead[n].second.
|
|
|
|
SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead;
|
|
|
|
|
|
|
|
// Constants can't be pointers to dynamically allocated memory.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator UI = GV->user_begin(), E = GV->user_end();
|
2012-07-24 15:21:08 +08:00
|
|
|
UI != E;) {
|
|
|
|
User *U = *UI++;
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
|
|
|
Value *V = SI->getValueOperand();
|
|
|
|
if (isa<Constant>(V)) {
|
|
|
|
Changed = true;
|
|
|
|
SI->eraseFromParent();
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
if (I->hasOneUse())
|
|
|
|
Dead.push_back(std::make_pair(I, SI));
|
|
|
|
}
|
|
|
|
} else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
|
|
|
|
if (isa<Constant>(MSI->getValue())) {
|
|
|
|
Changed = true;
|
|
|
|
MSI->eraseFromParent();
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
|
|
|
|
if (I->hasOneUse())
|
|
|
|
Dead.push_back(std::make_pair(I, MSI));
|
|
|
|
}
|
|
|
|
} else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
|
|
|
|
GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
|
|
|
|
if (MemSrc && MemSrc->isConstant()) {
|
|
|
|
Changed = true;
|
|
|
|
MTI->eraseFromParent();
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
|
|
|
|
if (I->hasOneUse())
|
|
|
|
Dead.push_back(std::make_pair(I, MTI));
|
|
|
|
}
|
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
|
|
|
|
if (CE->use_empty()) {
|
|
|
|
CE->destroyConstant();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
} else if (Constant *C = dyn_cast<Constant>(U)) {
|
2013-10-18 02:06:32 +08:00
|
|
|
if (isSafeToDestroyConstant(C)) {
|
2012-07-24 15:21:08 +08:00
|
|
|
C->destroyConstant();
|
|
|
|
// This could have invalidated UI, start over from scratch.
|
|
|
|
Dead.clear();
|
2012-08-29 23:32:21 +08:00
|
|
|
CleanupPointerRootUsers(GV, TLI);
|
2012-07-24 15:21:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0, e = Dead.size(); i != e; ++i) {
|
2012-08-29 23:32:21 +08:00
|
|
|
if (IsSafeComputationToRemove(Dead[i].first, TLI)) {
|
2012-07-24 15:21:08 +08:00
|
|
|
Dead[i].second->eraseFromParent();
|
|
|
|
Instruction *I = Dead[i].first;
|
|
|
|
do {
|
2013-01-12 07:08:52 +08:00
|
|
|
if (isAllocationFn(I, TLI))
|
|
|
|
break;
|
2012-07-24 15:21:08 +08:00
|
|
|
Instruction *J = dyn_cast<Instruction>(I->getOperand(0));
|
|
|
|
if (!J)
|
|
|
|
break;
|
|
|
|
I->eraseFromParent();
|
|
|
|
I = J;
|
2012-07-25 05:33:00 +08:00
|
|
|
} while (1);
|
2012-07-24 15:21:08 +08:00
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// We just marked GV constant. Loop over all users of the global, cleaning up
|
|
|
|
/// the obvious ones. This is largely just a quick scan over the use list to
|
|
|
|
/// clean up the easy and obvious cruft. This returns true if it made a change.
|
2012-02-12 09:13:18 +08:00
|
|
|
static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL,
|
2014-02-25 07:12:18 +08:00
|
|
|
TargetLibraryInfo *TLI) {
|
2004-10-11 00:43:46 +08:00
|
|
|
bool Changed = false;
|
2013-12-13 04:45:24 +08:00
|
|
|
// Note that we need to use a weak value handle for the worklist items. When
|
|
|
|
// we delete a constant array, we may also be holding pointer to one of its
|
|
|
|
// elements (or an element of one of its elements if we're dealing with an
|
|
|
|
// array of arrays) in the worklist.
|
2014-03-09 11:16:01 +08:00
|
|
|
SmallVector<WeakVH, 8> WorkList(V->user_begin(), V->user_end());
|
2013-04-02 16:16:45 +08:00
|
|
|
while (!WorkList.empty()) {
|
2013-12-13 04:45:24 +08:00
|
|
|
Value *UV = WorkList.pop_back_val();
|
|
|
|
if (!UV)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
User *U = cast<User>(UV);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-10-07 12:16:33 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
|
2005-02-28 02:58:52 +08:00
|
|
|
if (Init) {
|
|
|
|
// Replace the load with the initializer.
|
|
|
|
LI->replaceAllUsesWith(Init);
|
|
|
|
LI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
}
|
2004-10-07 12:16:33 +08:00
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
|
|
|
// Store must be unreachable or storing Init into the global.
|
2004-10-17 02:09:00 +08:00
|
|
|
SI->eraseFromParent();
|
2004-10-11 00:43:46 +08:00
|
|
|
Changed = true;
|
2004-10-07 12:16:33 +08:00
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Constant *SubInit = nullptr;
|
2005-09-26 15:34:35 +08:00
|
|
|
if (Init)
|
2009-10-06 00:36:26 +08:00
|
|
|
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
|
2014-02-21 08:06:31 +08:00
|
|
|
Changed |= CleanupConstantGlobalUsers(CE, SubInit, DL, TLI);
|
2014-01-03 04:01:43 +08:00
|
|
|
} else if ((CE->getOpcode() == Instruction::BitCast &&
|
|
|
|
CE->getType()->isPointerTy()) ||
|
|
|
|
CE->getOpcode() == Instruction::AddrSpaceCast) {
|
2005-02-28 02:58:52 +08:00
|
|
|
// Pointer cast, delete any stores and memsets to the global.
|
2014-04-25 13:29:35 +08:00
|
|
|
Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, TLI);
|
2004-10-07 12:16:33 +08:00
|
|
|
}
|
2005-02-28 02:58:52 +08:00
|
|
|
|
|
|
|
if (CE->use_empty()) {
|
|
|
|
CE->destroyConstant();
|
|
|
|
Changed = true;
|
2004-10-11 00:47:33 +08:00
|
|
|
}
|
2005-02-28 02:58:52 +08:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
|
2007-11-10 01:33:02 +08:00
|
|
|
// Do not transform "gepinst (gep constexpr (GV))" here, because forming
|
|
|
|
// "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
|
|
|
|
// and will invalidate our notion of what Init is.
|
2014-04-25 13:29:35 +08:00
|
|
|
Constant *SubInit = nullptr;
|
2007-11-10 01:33:02 +08:00
|
|
|
if (!isa<ConstantExpr>(GEP->getOperand(0))) {
|
2015-03-05 02:43:29 +08:00
|
|
|
ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(
|
2015-03-10 10:37:25 +08:00
|
|
|
ConstantFoldInstruction(GEP, DL, TLI));
|
2007-11-10 01:33:02 +08:00
|
|
|
if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
|
2009-10-06 00:36:26 +08:00
|
|
|
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
|
2012-03-28 22:50:09 +08:00
|
|
|
|
|
|
|
// If the initializer is an all-null value and we have an inbounds GEP,
|
|
|
|
// we already know what the result of any load from that GEP is.
|
|
|
|
// TODO: Handle splats.
|
|
|
|
if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
|
|
|
|
SubInit = Constant::getNullValue(GEP->getType()->getElementType());
|
2007-11-10 01:33:02 +08:00
|
|
|
}
|
2014-02-21 08:06:31 +08:00
|
|
|
Changed |= CleanupConstantGlobalUsers(GEP, SubInit, DL, TLI);
|
2004-10-11 00:47:33 +08:00
|
|
|
|
2004-10-11 00:43:46 +08:00
|
|
|
if (GEP->use_empty()) {
|
2004-10-17 02:09:00 +08:00
|
|
|
GEP->eraseFromParent();
|
2004-10-11 00:43:46 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
2005-02-28 02:58:52 +08:00
|
|
|
} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
|
|
|
|
if (MI->getRawDest() == V) {
|
|
|
|
MI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2004-10-09 04:59:28 +08:00
|
|
|
} else if (Constant *C = dyn_cast<Constant>(U)) {
|
|
|
|
// If we have a chain of dead constantexprs or other things dangling from
|
|
|
|
// us, and if they are all dead, nuke them without remorse.
|
2013-10-18 02:06:32 +08:00
|
|
|
if (isSafeToDestroyConstant(C)) {
|
2009-03-06 09:37:41 +08:00
|
|
|
C->destroyConstant();
|
2014-02-21 08:06:31 +08:00
|
|
|
CleanupConstantGlobalUsers(V, Init, DL, TLI);
|
2004-10-11 00:43:46 +08:00
|
|
|
return true;
|
2004-10-09 04:59:28 +08:00
|
|
|
}
|
2004-10-07 12:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
2004-10-11 00:43:46 +08:00
|
|
|
return Changed;
|
2004-10-07 12:16:33 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Return true if the specified instruction is a safe user of a derived
|
|
|
|
/// expression from a global that we want to SROA.
|
2008-01-14 10:09:12 +08:00
|
|
|
static bool isSafeSROAElementUse(Value *V) {
|
|
|
|
// We might have a dead and dangling constant hanging off of here.
|
|
|
|
if (Constant *C = dyn_cast<Constant>(V))
|
2013-10-18 02:06:32 +08:00
|
|
|
return isSafeToDestroyConstant(C);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I) return false;
|
|
|
|
|
|
|
|
// Loads are ok.
|
|
|
|
if (isa<LoadInst>(I)) return true;
|
|
|
|
|
|
|
|
// Stores *to* the pointer are ok.
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
|
|
|
return SI->getOperand(0) != V;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
// Otherwise, it must be a GEP.
|
|
|
|
GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!GEPI) return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
|
|
|
|
!cast<Constant>(GEPI->getOperand(1))->isNullValue())
|
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : GEPI->users())
|
|
|
|
if (!isSafeSROAElementUse(U))
|
2008-01-14 10:09:12 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2008-01-14 09:31:05 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// U is a direct user of the specified global value. Look at it and its uses
|
|
|
|
/// and decide whether it is safe to SROA this global.
|
2008-01-14 10:09:12 +08:00
|
|
|
static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
|
|
|
|
// The user of the global must be a GEP Inst or a ConstantExpr GEP.
|
2010-10-19 05:16:00 +08:00
|
|
|
if (!isa<GetElementPtrInst>(U) &&
|
|
|
|
(!isa<ConstantExpr>(U) ||
|
2008-01-14 10:09:12 +08:00
|
|
|
cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
|
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
// Check to see if this ConstantExpr GEP is SRA'able. In particular, we
|
|
|
|
// don't like < 3 operand CE's, and we don't like non-constant integer
|
|
|
|
// indices. This enforces that all uses are 'gep GV, 0, C, ...' for some
|
|
|
|
// value of C.
|
|
|
|
if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) ||
|
|
|
|
!cast<Constant>(U->getOperand(1))->isNullValue() ||
|
|
|
|
!isa<ConstantInt>(U->getOperand(2)))
|
|
|
|
return false;
|
2008-01-14 09:31:05 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
|
|
|
|
++GEPI; // Skip over the pointer index.
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
// If this is a use of an array allocation, do a bit more checking for sanity.
|
2011-07-18 12:54:35 +08:00
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) {
|
2008-01-14 10:09:12 +08:00
|
|
|
uint64_t NumElements = AT->getNumElements();
|
|
|
|
ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
// Check to make sure that index falls within the array. If not,
|
|
|
|
// something funny is going on, so we won't do the optimization.
|
|
|
|
//
|
|
|
|
if (Idx->getZExtValue() >= NumElements)
|
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
// We cannot scalar repl this level of the array unless any array
|
|
|
|
// sub-indices are in-range constants. In particular, consider:
|
|
|
|
// A[0][i]. We cannot know that the user isn't doing invalid things like
|
|
|
|
// allowing i to index an out-of-range subscript that accesses A[1].
|
|
|
|
//
|
|
|
|
// Scalar replacing *just* the outer index of the array is probably not
|
|
|
|
// going to be a win anyway, so just give up.
|
|
|
|
for (++GEPI; // Skip array index.
|
2009-08-18 22:58:19 +08:00
|
|
|
GEPI != E;
|
2008-01-14 10:09:12 +08:00
|
|
|
++GEPI) {
|
|
|
|
uint64_t NumElements;
|
2011-07-18 12:54:35 +08:00
|
|
|
if (ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI))
|
2008-01-14 10:09:12 +08:00
|
|
|
NumElements = SubArrayTy->getNumElements();
|
2011-07-18 12:54:35 +08:00
|
|
|
else if (VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI))
|
2009-08-18 22:58:19 +08:00
|
|
|
NumElements = SubVectorTy->getNumElements();
|
|
|
|
else {
|
2010-02-16 19:11:14 +08:00
|
|
|
assert((*GEPI)->isStructTy() &&
|
2009-08-18 22:58:19 +08:00
|
|
|
"Indexed GEP type is not array, vector, or struct!");
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
|
|
|
|
if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
|
2008-01-14 09:31:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-01-14 10:09:12 +08:00
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *UU : U->users())
|
|
|
|
if (!isSafeSROAElementUse(UU))
|
2008-01-14 10:09:12 +08:00
|
|
|
return false;
|
2014-03-09 11:16:01 +08:00
|
|
|
|
2008-01-14 10:09:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Look at all uses of the global and decide whether it is safe for us to
|
|
|
|
/// perform this transformation.
|
2008-01-14 10:09:12 +08:00
|
|
|
static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : GV->users())
|
|
|
|
if (!IsUserOfGlobalSafeForSRA(U, GV))
|
2008-01-14 10:09:12 +08:00
|
|
|
return false;
|
2014-03-09 11:16:01 +08:00
|
|
|
|
2008-01-14 09:31:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-01-14 09:31:05 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Perform scalar replacement of aggregates on the specified global variable.
|
|
|
|
/// This opens the door for other optimizations by exposing the behavior of the
|
|
|
|
/// program in a more fine-grained way. We have determined that this
|
|
|
|
/// transformation is safe already. We return the first global variable we
|
2004-10-09 01:32:09 +08:00
|
|
|
/// insert so that the caller can reprocess it.
|
2014-02-21 08:06:31 +08:00
|
|
|
static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
|
2008-01-14 09:31:05 +08:00
|
|
|
// Make sure this global only has simple uses that we can SRA.
|
2008-01-14 10:09:12 +08:00
|
|
|
if (!GlobalUsersSafeToSRA(GV))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-01-16 04:18:42 +08:00
|
|
|
assert(GV->hasLocalLinkage() && !GV->isConstant());
|
2004-10-09 01:32:09 +08:00
|
|
|
Constant *Init = GV->getInitializer();
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *Ty = Init->getType();
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-10-09 01:32:09 +08:00
|
|
|
std::vector<GlobalVariable*> NewGlobals;
|
|
|
|
Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
|
|
|
|
|
2008-04-26 15:40:11 +08:00
|
|
|
// Get the alignment of the global, either explicit or target-specific.
|
|
|
|
unsigned StartAlignment = GV->getAlignment();
|
|
|
|
if (StartAlignment == 0)
|
2014-02-21 08:06:31 +08:00
|
|
|
StartAlignment = DL.getABITypeAlignment(GV->getType());
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty)) {
|
2004-10-09 01:32:09 +08:00
|
|
|
NewGlobals.reserve(STy->getNumElements());
|
2014-02-21 08:06:31 +08:00
|
|
|
const StructLayout &Layout = *DL.getStructLayout(STy);
|
2004-10-09 01:32:09 +08:00
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
2012-01-25 14:48:06 +08:00
|
|
|
Constant *In = Init->getAggregateElement(i);
|
2004-10-09 01:32:09 +08:00
|
|
|
assert(In && "Couldn't get element of initializer?");
|
2009-11-06 12:27:31 +08:00
|
|
|
GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
|
2004-10-09 01:32:09 +08:00
|
|
|
GlobalVariable::InternalLinkage,
|
2009-07-31 01:37:43 +08:00
|
|
|
In, GV->getName()+"."+Twine(i),
|
2012-06-23 19:37:03 +08:00
|
|
|
GV->getThreadLocalMode(),
|
2009-07-08 09:26:06 +08:00
|
|
|
GV->getType()->getAddressSpace());
|
2015-11-10 00:47:16 +08:00
|
|
|
NGV->setExternallyInitialized(GV->isExternallyInitialized());
|
2015-12-23 03:16:50 +08:00
|
|
|
Globals.push_back(NGV);
|
2004-10-09 01:32:09 +08:00
|
|
|
NewGlobals.push_back(NGV);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-04-26 15:40:11 +08:00
|
|
|
// Calculate the known alignment of the field. If the original aggregate
|
|
|
|
// had 256 byte alignment for example, something might depend on that:
|
|
|
|
// propagate info to each field.
|
|
|
|
uint64_t FieldOffset = Layout.getElementOffset(i);
|
|
|
|
unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
|
2014-02-21 08:06:31 +08:00
|
|
|
if (NewAlign > DL.getABITypeAlignment(STy->getElementType(i)))
|
2008-04-26 15:40:11 +08:00
|
|
|
NGV->setAlignment(NewAlign);
|
2004-10-09 01:32:09 +08:00
|
|
|
}
|
2011-07-18 12:54:35 +08:00
|
|
|
} else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
|
2004-10-09 01:32:09 +08:00
|
|
|
unsigned NumElements = 0;
|
2011-07-18 12:54:35 +08:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(STy))
|
2004-10-09 01:32:09 +08:00
|
|
|
NumElements = ATy->getNumElements();
|
|
|
|
else
|
2008-04-26 15:40:11 +08:00
|
|
|
NumElements = cast<VectorType>(STy)->getNumElements();
|
2004-10-09 01:32:09 +08:00
|
|
|
|
2005-02-24 00:53:04 +08:00
|
|
|
if (NumElements > 16 && GV->hasNUsesOrMore(16))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr; // It's not worth it.
|
2004-10-09 01:32:09 +08:00
|
|
|
NewGlobals.reserve(NumElements);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-02-21 08:06:31 +08:00
|
|
|
uint64_t EltSize = DL.getTypeAllocSize(STy->getElementType());
|
|
|
|
unsigned EltAlign = DL.getABITypeAlignment(STy->getElementType());
|
2004-10-09 01:32:09 +08:00
|
|
|
for (unsigned i = 0, e = NumElements; i != e; ++i) {
|
2012-01-25 14:48:06 +08:00
|
|
|
Constant *In = Init->getAggregateElement(i);
|
2004-10-09 01:32:09 +08:00
|
|
|
assert(In && "Couldn't get element of initializer?");
|
|
|
|
|
2009-11-06 12:27:31 +08:00
|
|
|
GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
|
2004-10-09 01:32:09 +08:00
|
|
|
GlobalVariable::InternalLinkage,
|
2009-07-31 01:37:43 +08:00
|
|
|
In, GV->getName()+"."+Twine(i),
|
2012-06-23 19:37:03 +08:00
|
|
|
GV->getThreadLocalMode(),
|
2009-07-09 03:03:57 +08:00
|
|
|
GV->getType()->getAddressSpace());
|
2015-11-10 00:47:16 +08:00
|
|
|
NGV->setExternallyInitialized(GV->isExternallyInitialized());
|
2015-12-23 03:16:50 +08:00
|
|
|
Globals.push_back(NGV);
|
2004-10-09 01:32:09 +08:00
|
|
|
NewGlobals.push_back(NGV);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-04-26 15:40:11 +08:00
|
|
|
// Calculate the known alignment of the field. If the original aggregate
|
|
|
|
// had 256 byte alignment for example, something might depend on that:
|
|
|
|
// propagate info to each field.
|
|
|
|
unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
|
|
|
|
if (NewAlign > EltAlign)
|
|
|
|
NGV->setAlignment(NewAlign);
|
2004-10-09 01:32:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NewGlobals.empty())
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-10-28 22:30:53 +08:00
|
|
|
DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n");
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
|
2009-11-06 12:27:31 +08:00
|
|
|
Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
|
2004-10-09 01:32:09 +08:00
|
|
|
|
|
|
|
// Loop over all of the uses of the global, replacing the constantexpr geps,
|
|
|
|
// with smaller constantexpr geps or direct references.
|
|
|
|
while (!GV->use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
User *GEP = GV->user_back();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
assert(((isa<ConstantExpr>(GEP) &&
|
|
|
|
cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
|
|
|
|
isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-10-09 01:32:09 +08:00
|
|
|
// Ignore the 1th operand, which has to be zero or else the program is quite
|
|
|
|
// broken (undefined). Get the 2nd operand, which is the structure or array
|
|
|
|
// index.
|
2006-10-20 15:07:24 +08:00
|
|
|
unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
|
2004-10-09 01:32:09 +08:00
|
|
|
if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
|
|
|
|
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
Value *NewPtr = NewGlobals[Val];
|
2015-05-08 01:28:58 +08:00
|
|
|
Type *NewTy = NewGlobals[Val]->getValueType();
|
2004-10-09 01:32:09 +08:00
|
|
|
|
|
|
|
// Form a shorter GEP if needed.
|
2008-02-20 19:26:25 +08:00
|
|
|
if (GEP->getNumOperands() > 3) {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
|
2007-01-31 12:40:53 +08:00
|
|
|
SmallVector<Constant*, 8> Idxs;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
Idxs.push_back(NullInt);
|
|
|
|
for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
|
|
|
|
Idxs.push_back(CE->getOperand(i));
|
2015-04-03 02:55:32 +08:00
|
|
|
NewPtr =
|
|
|
|
ConstantExpr::getGetElementPtr(NewTy, cast<Constant>(NewPtr), Idxs);
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
} else {
|
|
|
|
GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
|
2007-02-01 03:59:55 +08:00
|
|
|
SmallVector<Value*, 8> Idxs;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
Idxs.push_back(NullInt);
|
|
|
|
for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
|
|
|
|
Idxs.push_back(GEPI->getOperand(i));
|
2015-03-14 09:53:18 +08:00
|
|
|
NewPtr = GetElementPtrInst::Create(
|
2015-05-08 01:28:58 +08:00
|
|
|
NewTy, NewPtr, Idxs, GEPI->getName() + "." + Twine(Val), GEPI);
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
}
|
2008-02-20 19:26:25 +08:00
|
|
|
}
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
GEP->replaceAllUsesWith(NewPtr);
|
|
|
|
|
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
|
2004-10-17 02:09:00 +08:00
|
|
|
GEPI->eraseFromParent();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
else
|
|
|
|
cast<ConstantExpr>(GEP)->destroyConstant();
|
2004-10-09 01:32:09 +08:00
|
|
|
}
|
|
|
|
|
2004-10-09 04:25:55 +08:00
|
|
|
// Delete the old global, now that it is dead.
|
|
|
|
Globals.erase(GV);
|
2004-10-09 01:32:09 +08:00
|
|
|
++NumSRA;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
|
|
|
|
// Loop over the new globals array deleting any globals that are obviously
|
|
|
|
// dead. This can arise due to scalarization of a structure or an array that
|
|
|
|
// has elements that are dead.
|
|
|
|
unsigned FirstGlobal = 0;
|
|
|
|
for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
|
|
|
|
if (NewGlobals[i]->use_empty()) {
|
|
|
|
Globals.erase(NewGlobals[i]);
|
|
|
|
if (FirstGlobal == i) ++FirstGlobal;
|
|
|
|
}
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : nullptr;
|
2004-10-09 01:32:09 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Return true if all users of the specified value will trap if the value is
|
|
|
|
/// dynamically null. PHIs keeps track of any phi nodes we've seen to avoid
|
|
|
|
/// reprocessing them.
|
2010-04-07 03:24:18 +08:00
|
|
|
static bool AllUsesOfValueWillTrapIfNull(const Value *V,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<const PHINode*> &PHIs) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : V->users())
|
2010-04-07 03:14:05 +08:00
|
|
|
if (isa<LoadInst>(U)) {
|
2004-10-10 05:48:45 +08:00
|
|
|
// Will trap.
|
2010-04-07 03:24:18 +08:00
|
|
|
} else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
2004-10-10 05:48:45 +08:00
|
|
|
if (SI->getOperand(0) == V) {
|
2010-04-07 03:14:05 +08:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-10 05:48:45 +08:00
|
|
|
return false; // Storing the value.
|
|
|
|
}
|
2010-04-07 03:24:18 +08:00
|
|
|
} else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
|
2010-03-21 05:00:25 +08:00
|
|
|
if (CI->getCalledValue() != V) {
|
2010-04-07 03:14:05 +08:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-10 05:48:45 +08:00
|
|
|
return false; // Not calling the ptr
|
|
|
|
}
|
2010-04-07 03:24:18 +08:00
|
|
|
} else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
|
2010-03-21 05:00:25 +08:00
|
|
|
if (II->getCalledValue() != V) {
|
2010-04-07 03:14:05 +08:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-10 05:48:45 +08:00
|
|
|
return false; // Not calling the ptr
|
|
|
|
}
|
2010-04-07 03:24:18 +08:00
|
|
|
} else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
|
2007-09-14 00:30:19 +08:00
|
|
|
if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
|
2010-04-07 03:24:18 +08:00
|
|
|
} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
|
2007-09-14 00:30:19 +08:00
|
|
|
if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
|
2010-04-07 03:24:18 +08:00
|
|
|
} else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
|
2007-09-14 00:30:19 +08:00
|
|
|
// If we've already seen this phi node, ignore it, it has already been
|
|
|
|
// checked.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
|
2010-01-30 07:54:14 +08:00
|
|
|
return false;
|
2010-04-07 03:14:05 +08:00
|
|
|
} else if (isa<ICmpInst>(U) &&
|
2014-03-09 11:16:01 +08:00
|
|
|
isa<ConstantPointerNull>(U->getOperand(1))) {
|
2010-02-25 14:39:10 +08:00
|
|
|
// Ignore icmp X, null
|
2004-10-10 05:48:45 +08:00
|
|
|
} else {
|
2010-04-07 03:14:05 +08:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-10 05:48:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-09 11:16:01 +08:00
|
|
|
|
2004-10-10 05:48:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Return true if all uses of any loads from GV will trap if the loaded value
|
|
|
|
/// is null. Note that this also permits comparisons of the loaded value
|
|
|
|
/// against null, as a special case.
|
2010-04-07 03:24:18 +08:00
|
|
|
static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : GV->users())
|
2010-04-07 03:24:18 +08:00
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
|
|
|
|
SmallPtrSet<const PHINode*, 8> PHIs;
|
2007-09-14 00:30:19 +08:00
|
|
|
if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
|
2004-10-10 05:48:45 +08:00
|
|
|
return false;
|
2010-04-07 03:14:05 +08:00
|
|
|
} else if (isa<StoreInst>(U)) {
|
2004-10-10 05:48:45 +08:00
|
|
|
// Ignore stores to the global.
|
|
|
|
} else {
|
|
|
|
// We don't know or understand this user, bail out.
|
2010-04-07 03:14:05 +08:00
|
|
|
//cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
|
2004-10-10 05:48:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-06 12:27:31 +08:00
|
|
|
static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
|
2004-10-11 07:14:11 +08:00
|
|
|
bool Changed = false;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
|
2004-10-11 07:14:11 +08:00
|
|
|
Instruction *I = cast<Instruction>(*UI++);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
|
|
|
LI->setOperand(0, NewV);
|
|
|
|
Changed = true;
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
|
|
|
if (SI->getOperand(1) == V) {
|
|
|
|
SI->setOperand(1, NewV);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
} else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
|
2010-04-07 02:45:08 +08:00
|
|
|
CallSite CS(I);
|
|
|
|
if (CS.getCalledValue() == V) {
|
2004-10-11 07:14:11 +08:00
|
|
|
// Calling through the pointer! Turn into a direct call, but be careful
|
|
|
|
// that the pointer is not also being passed as an argument.
|
2010-04-07 02:45:08 +08:00
|
|
|
CS.setCalledFunction(NewV);
|
2004-10-11 07:14:11 +08:00
|
|
|
Changed = true;
|
|
|
|
bool PassedAsArg = false;
|
2010-04-07 02:45:08 +08:00
|
|
|
for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
|
|
|
|
if (CS.getArgument(i) == V) {
|
2004-10-11 07:14:11 +08:00
|
|
|
PassedAsArg = true;
|
2010-04-07 02:45:08 +08:00
|
|
|
CS.setArgument(i, NewV);
|
2004-10-11 07:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PassedAsArg) {
|
|
|
|
// Being passed as an argument also. Be careful to not invalidate UI!
|
2014-03-09 11:16:01 +08:00
|
|
|
UI = V->user_begin();
|
2004-10-11 07:14:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(I)) {
|
|
|
|
Changed |= OptimizeAwayTrappingUsesOfValue(CI,
|
2009-07-30 02:55:55 +08:00
|
|
|
ConstantExpr::getCast(CI->getOpcode(),
|
2009-11-06 12:27:31 +08:00
|
|
|
NewV, CI->getType()));
|
2004-10-11 07:14:11 +08:00
|
|
|
if (CI->use_empty()) {
|
|
|
|
Changed = true;
|
2004-10-17 02:09:00 +08:00
|
|
|
CI->eraseFromParent();
|
2004-10-11 07:14:11 +08:00
|
|
|
}
|
|
|
|
} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
// Should handle GEP here.
|
2007-01-31 12:40:53 +08:00
|
|
|
SmallVector<Constant*, 8> Idxs;
|
|
|
|
Idxs.reserve(GEPI->getNumOperands()-1);
|
2008-05-29 09:59:18 +08:00
|
|
|
for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
|
|
|
|
i != e; ++i)
|
|
|
|
if (Constant *C = dyn_cast<Constant>(*i))
|
2007-01-31 12:40:53 +08:00
|
|
|
Idxs.push_back(C);
|
2004-10-11 07:14:11 +08:00
|
|
|
else
|
|
|
|
break;
|
2007-01-31 12:40:53 +08:00
|
|
|
if (Idxs.size() == GEPI->getNumOperands()-1)
|
2015-04-03 02:55:32 +08:00
|
|
|
Changed |= OptimizeAwayTrappingUsesOfValue(
|
|
|
|
GEPI, ConstantExpr::getGetElementPtr(nullptr, NewV, Idxs));
|
2004-10-11 07:14:11 +08:00
|
|
|
if (GEPI->use_empty()) {
|
|
|
|
Changed = true;
|
2004-10-17 02:09:00 +08:00
|
|
|
GEPI->eraseFromParent();
|
2004-10-11 07:14:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// The specified global has only one non-null value stored into it. If there
|
|
|
|
/// are uses of the loaded value that would trap if the loaded value is
|
|
|
|
/// dynamically null, then we know that they cannot be reachable with a null
|
|
|
|
/// optimize away the load.
|
2012-02-12 09:13:18 +08:00
|
|
|
static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL,
|
2012-02-12 09:13:18 +08:00
|
|
|
TargetLibraryInfo *TLI) {
|
2004-10-11 07:14:11 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-01-14 08:12:58 +08:00
|
|
|
// Keep track of whether we are able to remove all the uses of the global
|
|
|
|
// other than the store that defines it.
|
|
|
|
bool AllNonStoreUsesGone = true;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2004-10-11 07:14:11 +08:00
|
|
|
// Replace all uses of loads with uses of uses of the stored value.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator GUI = GV->user_begin(), E = GV->user_end(); GUI != E;){
|
2009-01-14 08:12:58 +08:00
|
|
|
User *GlobalUser = *GUI++;
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
|
2009-11-06 12:27:31 +08:00
|
|
|
Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
|
2009-01-14 08:12:58 +08:00
|
|
|
// If we were able to delete all uses of the loads
|
|
|
|
if (LI->use_empty()) {
|
|
|
|
LI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
} else {
|
|
|
|
AllNonStoreUsesGone = false;
|
|
|
|
}
|
|
|
|
} else if (isa<StoreInst>(GlobalUser)) {
|
|
|
|
// Ignore the store that stores "LV" to the global.
|
|
|
|
assert(GlobalUser->getOperand(1) == GV &&
|
|
|
|
"Must be storing *to* the global");
|
2004-10-11 07:14:11 +08:00
|
|
|
} else {
|
2009-01-14 08:12:58 +08:00
|
|
|
AllNonStoreUsesGone = false;
|
|
|
|
|
|
|
|
// If we get here we could have other crazy uses that are transitively
|
|
|
|
// loaded.
|
|
|
|
assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
|
2012-09-28 18:01:27 +08:00
|
|
|
isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
|
|
|
|
isa<BitCastInst>(GlobalUser) ||
|
|
|
|
isa<GetElementPtrInst>(GlobalUser)) &&
|
2011-05-22 15:15:13 +08:00
|
|
|
"Only expect load and stores!");
|
2004-10-11 07:14:11 +08:00
|
|
|
}
|
2009-01-14 08:12:58 +08:00
|
|
|
}
|
2004-10-11 07:14:11 +08:00
|
|
|
|
|
|
|
if (Changed) {
|
2015-10-28 22:30:53 +08:00
|
|
|
DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV << "\n");
|
2004-10-11 07:14:11 +08:00
|
|
|
++NumGlobUses;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we nuked all of the loads, then none of the stores are needed either,
|
|
|
|
// nor is the global.
|
2009-01-14 08:12:58 +08:00
|
|
|
if (AllNonStoreUsesGone) {
|
2012-07-24 15:21:08 +08:00
|
|
|
if (isLeakCheckerRoot(GV)) {
|
2012-08-29 23:32:21 +08:00
|
|
|
Changed |= CleanupPointerRootUsers(GV, TLI);
|
2012-07-24 15:21:08 +08:00
|
|
|
} else {
|
|
|
|
Changed = true;
|
2014-04-25 13:29:35 +08:00
|
|
|
CleanupConstantGlobalUsers(GV, nullptr, DL, TLI);
|
2012-07-24 15:21:08 +08:00
|
|
|
}
|
2004-10-11 07:14:11 +08:00
|
|
|
if (GV->use_empty()) {
|
2012-07-24 15:21:08 +08:00
|
|
|
DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
|
|
|
|
Changed = true;
|
2004-10-17 02:09:00 +08:00
|
|
|
GV->eraseFromParent();
|
2004-10-11 07:14:11 +08:00
|
|
|
++NumDeleted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Walk the use list of V, constant folding all of the instructions that are
|
|
|
|
/// foldable.
|
2015-03-05 02:43:29 +08:00
|
|
|
static void ConstantPropUsersOf(Value *V, const DataLayout &DL,
|
2014-02-25 07:12:18 +08:00
|
|
|
TargetLibraryInfo *TLI) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(*UI++))
|
2015-03-10 10:37:25 +08:00
|
|
|
if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
I->replaceAllUsesWith(NewC);
|
|
|
|
|
2005-02-01 09:23:31 +08:00
|
|
|
// Advance UI to the next non-I use to avoid invalidating it!
|
|
|
|
// Instructions could multiply use V.
|
|
|
|
while (UI != E && *UI == I)
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
++UI;
|
2005-02-01 09:23:31 +08:00
|
|
|
I->eraseFromParent();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// This function takes the specified global variable, and transforms the
|
|
|
|
/// program as if it always contained the result of the specified malloc.
|
|
|
|
/// Because it is always the result of the specified malloc, there is no reason
|
|
|
|
/// to actually DO the malloc. Instead, turn the malloc into a global, and any
|
|
|
|
/// loads of GV as uses of the new global.
|
2015-03-05 02:43:29 +08:00
|
|
|
static GlobalVariable *
|
|
|
|
OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, CallInst *CI, Type *AllocTy,
|
|
|
|
ConstantInt *NElements, const DataLayout &DL,
|
|
|
|
TargetLibraryInfo *TLI) {
|
2010-02-26 06:33:52 +08:00
|
|
|
DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *GlobalType;
|
2010-02-26 06:33:52 +08:00
|
|
|
if (NElements->getZExtValue() == 1)
|
|
|
|
GlobalType = AllocTy;
|
|
|
|
else
|
|
|
|
// If we have an array allocation, the global variable is of an array.
|
|
|
|
GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue());
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
// Create the new global variable. The contents of the malloc'd memory is
|
|
|
|
// undefined, so initialize with an undef value.
|
2015-12-23 03:16:50 +08:00
|
|
|
GlobalVariable *NewGV = new GlobalVariable(
|
|
|
|
*GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage,
|
|
|
|
UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,
|
|
|
|
GV->getThreadLocalMode());
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-02-26 06:33:52 +08:00
|
|
|
// If there are bitcast users of the malloc (which is typical, usually we have
|
|
|
|
// a malloc + bitcast) then replace them with uses of the new global. Update
|
|
|
|
// other users to use the global as well.
|
2014-04-25 13:29:35 +08:00
|
|
|
BitCastInst *TheBC = nullptr;
|
2010-02-26 06:33:52 +08:00
|
|
|
while (!CI->use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
Instruction *User = cast<Instruction>(CI->user_back());
|
2010-02-26 06:33:52 +08:00
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
|
|
|
|
if (BCI->getType() == NewGV->getType()) {
|
|
|
|
BCI->replaceAllUsesWith(NewGV);
|
|
|
|
BCI->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
BCI->setOperand(0, NewGV);
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!TheBC)
|
2010-02-26 06:33:52 +08:00
|
|
|
TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
|
|
|
|
User->replaceUsesOfWith(CI, TheBC);
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
Constant *RepValue = NewGV;
|
2016-01-17 04:30:46 +08:00
|
|
|
if (NewGV->getType() != GV->getValueType())
|
|
|
|
RepValue = ConstantExpr::getBitCast(RepValue, GV->getValueType());
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
// If there is a comparison against null, we will insert a global bool to
|
|
|
|
// keep track of whether the global was initialized yet or not.
|
|
|
|
GlobalVariable *InitBool =
|
2009-11-06 12:27:31 +08:00
|
|
|
new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
|
2009-09-19 06:35:49 +08:00
|
|
|
GlobalValue::InternalLinkage,
|
2009-11-06 12:27:31 +08:00
|
|
|
ConstantInt::getFalse(GV->getContext()),
|
2012-06-23 19:37:03 +08:00
|
|
|
GV->getName()+".init", GV->getThreadLocalMode());
|
2009-09-19 06:35:49 +08:00
|
|
|
bool InitBoolUsed = false;
|
|
|
|
|
|
|
|
// Loop over all uses of GV, processing them in turn.
|
2010-02-26 06:33:52 +08:00
|
|
|
while (!GV->use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) {
|
2009-09-19 06:35:49 +08:00
|
|
|
// The global is initialized when the store to it occurs.
|
2012-02-06 03:56:38 +08:00
|
|
|
new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
|
|
|
|
SI->getOrdering(), SI->getSynchScope(), SI);
|
2009-09-19 06:35:49 +08:00
|
|
|
SI->eraseFromParent();
|
2010-02-26 06:33:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
LoadInst *LI = cast<LoadInst>(GV->user_back());
|
2010-02-26 06:33:52 +08:00
|
|
|
while (!LI->use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
Use &LoadUse = *LI->use_begin();
|
|
|
|
ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
|
|
|
|
if (!ICI) {
|
2010-02-26 06:33:52 +08:00
|
|
|
LoadUse = RepValue;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-02-26 06:33:52 +08:00
|
|
|
// Replace the cmp X, 0 with a use of the bool value.
|
2012-02-06 03:56:38 +08:00
|
|
|
// Sink the load to where the compare was, if atomic rules allow us to.
|
|
|
|
Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
|
|
|
|
LI->getOrdering(), LI->getSynchScope(),
|
|
|
|
LI->isUnordered() ? (Instruction*)ICI : LI);
|
2010-02-26 06:33:52 +08:00
|
|
|
InitBoolUsed = true;
|
|
|
|
switch (ICI->getPredicate()) {
|
|
|
|
default: llvm_unreachable("Unknown ICmp Predicate!");
|
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
case ICmpInst::ICMP_SLT: // X < null -> always false
|
|
|
|
LV = ConstantInt::getFalse(GV->getContext());
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
case ICmpInst::ICMP_EQ:
|
|
|
|
LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
break; // no change.
|
|
|
|
}
|
|
|
|
ICI->replaceAllUsesWith(LV);
|
|
|
|
ICI->eraseFromParent();
|
2009-09-19 06:35:49 +08:00
|
|
|
}
|
2010-02-26 06:33:52 +08:00
|
|
|
LI->eraseFromParent();
|
|
|
|
}
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
// If the initialization boolean was used, insert it, otherwise delete it.
|
|
|
|
if (!InitBoolUsed) {
|
|
|
|
while (!InitBool->use_empty()) // Delete initializations
|
2014-03-09 11:16:01 +08:00
|
|
|
cast<StoreInst>(InitBool->user_back())->eraseFromParent();
|
2009-09-19 06:35:49 +08:00
|
|
|
delete InitBool;
|
|
|
|
} else
|
2015-10-14 01:51:03 +08:00
|
|
|
GV->getParent()->getGlobalList().insert(GV->getIterator(), InitBool);
|
2009-09-19 06:35:49 +08:00
|
|
|
|
2010-02-26 06:33:52 +08:00
|
|
|
// Now the GV is dead, nuke it and the malloc..
|
2009-09-19 06:35:49 +08:00
|
|
|
GV->eraseFromParent();
|
|
|
|
CI->eraseFromParent();
|
|
|
|
|
|
|
|
// To further other optimizations, loop over all users of NewGV and try to
|
|
|
|
// constant prop them. This will promote GEP instructions with constant
|
|
|
|
// indices into GEP constant-exprs, which will allow global-opt to hack on it.
|
2014-02-21 08:06:31 +08:00
|
|
|
ConstantPropUsersOf(NewGV, DL, TLI);
|
2009-09-19 06:35:49 +08:00
|
|
|
if (RepValue != NewGV)
|
2014-02-21 08:06:31 +08:00
|
|
|
ConstantPropUsersOf(RepValue, DL, TLI);
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
return NewGV;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Scan the use-list of V checking to make sure that there are no complex uses
|
|
|
|
/// of V. We permit simple things like dereferencing the pointer, but not
|
|
|
|
/// storing through the address, unless it is to the specified global.
|
2010-04-07 02:58:22 +08:00
|
|
|
static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V,
|
|
|
|
const GlobalVariable *GV,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<const PHINode*> &PHIs) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : V->users()) {
|
|
|
|
const Instruction *Inst = cast<Instruction>(U);
|
2010-04-07 03:14:05 +08:00
|
|
|
|
2008-12-16 05:08:54 +08:00
|
|
|
if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
|
|
|
|
continue; // Fine, ignore.
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-04-07 02:58:22 +08:00
|
|
|
if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
2004-12-02 15:11:07 +08:00
|
|
|
if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
|
|
|
|
return false; // Storing the pointer itself... bad.
|
2008-12-16 05:08:54 +08:00
|
|
|
continue; // Otherwise, storing through it, or storing into GV... fine.
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-04-11 02:19:22 +08:00
|
|
|
// Must index into the array and into the struct.
|
|
|
|
if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) {
|
2008-12-16 05:08:54 +08:00
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
|
2004-12-02 15:11:07 +08:00
|
|
|
return false;
|
2008-12-16 05:08:54 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-04-07 02:58:22 +08:00
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(Inst)) {
|
2007-09-14 00:37:20 +08:00
|
|
|
// PHIs are ok if all uses are ok. Don't infinitely recurse through PHI
|
|
|
|
// cycles.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (PHIs.insert(PN).second)
|
2007-09-14 11:41:21 +08:00
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs))
|
|
|
|
return false;
|
2008-12-16 05:08:54 +08:00
|
|
|
continue;
|
2004-12-02 15:11:07 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-04-07 02:58:22 +08:00
|
|
|
if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
|
2008-12-16 05:08:54 +08:00
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-16 05:08:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
2004-12-02 15:11:07 +08:00
|
|
|
return true;
|
2006-10-01 07:32:09 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// The Alloc pointer is stored into GV somewhere. Transform all uses of the
|
|
|
|
/// allocation into loads from the global and uses of the resultant pointer.
|
|
|
|
/// Further, delete the store into GV. This assumes that these value pass the
|
2006-10-01 07:32:09 +08:00
|
|
|
/// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
|
2010-10-19 05:16:00 +08:00
|
|
|
static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
|
2006-10-01 07:32:09 +08:00
|
|
|
GlobalVariable *GV) {
|
|
|
|
while (!Alloc->use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
Instruction *U = cast<Instruction>(*Alloc->user_begin());
|
2007-09-14 02:00:31 +08:00
|
|
|
Instruction *InsertPt = U;
|
2006-10-01 07:32:09 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
|
|
|
// If this is the store of the allocation into the global, remove it.
|
|
|
|
if (SI->getOperand(1) == GV) {
|
|
|
|
SI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-14 02:00:31 +08:00
|
|
|
} else if (PHINode *PN = dyn_cast<PHINode>(U)) {
|
|
|
|
// Insert the load in the corresponding predecessor, not right before the
|
|
|
|
// PHI.
|
2014-03-09 11:16:01 +08:00
|
|
|
InsertPt = PN->getIncomingBlock(*Alloc->use_begin())->getTerminator();
|
2008-12-16 05:44:34 +08:00
|
|
|
} else if (isa<BitCastInst>(U)) {
|
|
|
|
// Must be bitcast between the malloc and store to initialize the global.
|
|
|
|
ReplaceUsesOfMallocWithGlobal(U, GV);
|
|
|
|
U->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
|
|
|
|
// If this is a "GEP bitcast" and the user is a store to the global, then
|
|
|
|
// just process it as a bitcast.
|
|
|
|
if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
|
2014-03-09 11:16:01 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->user_back()))
|
2008-12-16 05:44:34 +08:00
|
|
|
if (SI->getOperand(1) == GV) {
|
|
|
|
// Must be bitcast GEP between the malloc and store to initialize
|
|
|
|
// the global.
|
|
|
|
ReplaceUsesOfMallocWithGlobal(GEPI, GV);
|
|
|
|
GEPI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-01 07:32:09 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2006-10-01 07:32:09 +08:00
|
|
|
// Insert a load from the global, and use it instead of the malloc.
|
2007-09-14 02:00:31 +08:00
|
|
|
Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
|
2006-10-01 07:32:09 +08:00
|
|
|
U->replaceUsesOfWith(Alloc, NL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Verify that all uses of V (a load, or a phi of a load) are simple enough to
|
|
|
|
/// perform heap SRA on. This permits GEP's that index through the array and
|
|
|
|
/// struct field, icmps of null, and PHIs.
|
2010-04-01 16:21:08 +08:00
|
|
|
static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<const PHINode*> &LoadUsingPHIs,
|
|
|
|
SmallPtrSetImpl<const PHINode*> &LoadUsingPHIsPerLoad) {
|
2008-12-17 05:24:51 +08:00
|
|
|
// We permit two users of the load: setcc comparing against the null
|
|
|
|
// pointer, and a getelementptr of a specific form.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : V->users()) {
|
|
|
|
const Instruction *UI = cast<Instruction>(U);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 05:24:51 +08:00
|
|
|
// Comparison against null is ok.
|
2014-03-09 11:16:01 +08:00
|
|
|
if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UI)) {
|
2008-12-17 05:24:51 +08:00
|
|
|
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 05:24:51 +08:00
|
|
|
// getelementptr is also ok, but only a simple form.
|
2014-03-09 11:16:01 +08:00
|
|
|
if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) {
|
2008-12-17 05:24:51 +08:00
|
|
|
// Must index into the array and into the struct.
|
|
|
|
if (GEPI->getNumOperands() < 3)
|
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 05:24:51 +08:00
|
|
|
// Otherwise the GEP is ok.
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!LoadUsingPHIsPerLoad.insert(PN).second)
|
2009-06-02 08:56:07 +08:00
|
|
|
// This means some phi nodes are dependent on each other.
|
|
|
|
// Avoid infinite looping!
|
|
|
|
return false;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!LoadUsingPHIs.insert(PN).second)
|
2009-06-02 08:56:07 +08:00
|
|
|
// If we have already analyzed this PHI, then it is safe.
|
2008-12-17 05:24:51 +08:00
|
|
|
continue;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// Make sure all uses of the PHI are simple enough to transform.
|
2009-06-02 08:56:07 +08:00
|
|
|
if (!LoadUsesSimpleEnoughForHeapSRA(PN,
|
|
|
|
LoadUsingPHIs, LoadUsingPHIsPerLoad))
|
2008-12-17 05:24:51 +08:00
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 05:24:51 +08:00
|
|
|
continue;
|
2006-10-01 07:32:09 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 05:24:51 +08:00
|
|
|
// Otherwise we don't know what this is, not ok.
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 05:24:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// If all users of values loaded from GV are simple enough to perform HeapSRA,
|
|
|
|
/// return true.
|
2010-04-01 16:21:08 +08:00
|
|
|
static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV,
|
2009-09-19 06:35:49 +08:00
|
|
|
Instruction *StoredVal) {
|
2010-04-01 16:21:08 +08:00
|
|
|
SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
|
|
|
|
SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : GV->users())
|
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
|
2009-06-02 08:56:07 +08:00
|
|
|
if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
|
|
|
|
LoadUsingPHIsPerLoad))
|
2008-12-17 13:28:49 +08:00
|
|
|
return false;
|
2009-06-02 08:56:07 +08:00
|
|
|
LoadUsingPHIsPerLoad.clear();
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// If we reach here, we know that all uses of the loads and transitive uses
|
|
|
|
// (through PHI nodes) are simple enough to transform. However, we don't know
|
2010-10-19 05:16:00 +08:00
|
|
|
// that all inputs the to the PHI nodes are in the same equivalence sets.
|
2008-12-17 13:28:49 +08:00
|
|
|
// Check to verify that all operands of the PHIs are either PHIS that can be
|
|
|
|
// transformed, loads from GV, or MI itself.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (const PHINode *PN : LoadUsingPHIs) {
|
2008-12-17 13:28:49 +08:00
|
|
|
for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
|
|
|
|
Value *InVal = PN->getIncomingValue(op);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// PHI of the stored value itself is ok.
|
2009-09-19 06:35:49 +08:00
|
|
|
if (InVal == StoredVal) continue;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-04-01 16:21:08 +08:00
|
|
|
if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) {
|
2008-12-17 13:28:49 +08:00
|
|
|
// One of the PHIs in our set is (optimistically) ok.
|
|
|
|
if (LoadUsingPHIs.count(InPN))
|
|
|
|
continue;
|
2008-12-17 05:24:51 +08:00
|
|
|
return false;
|
2008-12-17 13:28:49 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// Load from GV is ok.
|
2010-04-01 16:21:08 +08:00
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(InVal))
|
2008-12-17 13:28:49 +08:00
|
|
|
if (LI->getOperand(0) == GV)
|
|
|
|
continue;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// UNDEF? NULL?
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// Anything else is rejected.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2006-10-01 07:32:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
|
|
|
|
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
|
2009-11-06 12:27:31 +08:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
|
2008-12-17 13:28:49 +08:00
|
|
|
std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
if (FieldNo >= FieldVals.size())
|
|
|
|
FieldVals.resize(FieldNo+1);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// If we already have this value, just reuse the previously scalarized
|
|
|
|
// version.
|
|
|
|
if (Value *FieldVal = FieldVals[FieldNo])
|
|
|
|
return FieldVal;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// Depending on what instruction this is, we have several cases.
|
|
|
|
Value *Result;
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
|
|
|
|
// This is a scalarized version of the load from the global. Just create
|
|
|
|
// a new Load of the scalarized global.
|
|
|
|
Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
|
|
|
|
InsertedScalarizedValues,
|
2009-11-06 12:27:31 +08:00
|
|
|
PHIsToRewrite),
|
2009-07-31 01:37:43 +08:00
|
|
|
LI->getName()+".f"+Twine(FieldNo), LI);
|
2015-03-14 09:53:18 +08:00
|
|
|
} else {
|
|
|
|
PHINode *PN = cast<PHINode>(V);
|
2008-12-17 13:28:49 +08:00
|
|
|
// PN's type is pointer to struct. Make a new PHI of pointer to struct
|
|
|
|
// field.
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-04-24 04:36:10 +08:00
|
|
|
PointerType *PTy = cast<PointerType>(PN->getType());
|
|
|
|
StructType *ST = cast<StructType>(PTy->getElementType());
|
|
|
|
|
|
|
|
unsigned AS = PTy->getAddressSpace();
|
2011-03-30 19:19:20 +08:00
|
|
|
PHINode *NewPN =
|
2014-04-24 04:36:10 +08:00
|
|
|
PHINode::Create(PointerType::get(ST->getElementType(FieldNo), AS),
|
2011-03-30 19:28:46 +08:00
|
|
|
PN->getNumIncomingValues(),
|
2009-07-31 01:37:43 +08:00
|
|
|
PN->getName()+".f"+Twine(FieldNo), PN);
|
2011-03-30 19:19:20 +08:00
|
|
|
Result = NewPN;
|
2008-12-17 13:28:49 +08:00
|
|
|
PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
return FieldVals[FieldNo] = Result;
|
2007-09-14 02:00:31 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Given a load instruction and a value derived from the load, rewrite the
|
|
|
|
/// derived value to use the HeapSRoA'd load.
|
2010-10-19 05:16:00 +08:00
|
|
|
static void RewriteHeapSROALoadUser(Instruction *LoadUser,
|
2008-12-17 13:28:49 +08:00
|
|
|
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
|
2009-11-06 12:27:31 +08:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
|
2007-09-14 01:29:05 +08:00
|
|
|
// If this is a comparison against null, handle it.
|
|
|
|
if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
|
|
|
|
assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
|
|
|
|
// If we have a setcc of the loaded pointer, we can use a setcc of any
|
|
|
|
// field.
|
2008-12-17 13:28:49 +08:00
|
|
|
Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
|
2009-11-06 12:27:31 +08:00
|
|
|
InsertedScalarizedValues, PHIsToRewrite);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-07-10 07:48:35 +08:00
|
|
|
Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
|
2010-10-19 05:16:00 +08:00
|
|
|
Constant::getNullValue(NPtr->getType()),
|
2009-07-10 07:48:35 +08:00
|
|
|
SCI->getName());
|
2007-09-14 01:29:05 +08:00
|
|
|
SCI->replaceAllUsesWith(New);
|
|
|
|
SCI->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
|
2007-09-14 02:00:31 +08:00
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
|
|
|
|
assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
|
|
|
|
&& "Unexpected GEPI!");
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2007-09-14 02:00:31 +08:00
|
|
|
// Load the pointer for this field.
|
|
|
|
unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
|
2008-12-17 13:28:49 +08:00
|
|
|
Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
|
2009-11-06 12:27:31 +08:00
|
|
|
InsertedScalarizedValues, PHIsToRewrite);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2007-09-14 02:00:31 +08:00
|
|
|
// Create the new GEP idx vector.
|
|
|
|
SmallVector<Value*, 8> GEPIdx;
|
|
|
|
GEPIdx.push_back(GEPI->getOperand(1));
|
|
|
|
GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-03-15 03:24:04 +08:00
|
|
|
Value *NGEPI = GetElementPtrInst::Create(GEPI->getResultElementType(), NewPtr, GEPIdx,
|
2008-04-07 04:25:17 +08:00
|
|
|
GEPI->getName(), GEPI);
|
2007-09-14 02:00:31 +08:00
|
|
|
GEPI->replaceAllUsesWith(NGEPI);
|
|
|
|
GEPI->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2008-12-17 13:28:49 +08:00
|
|
|
|
|
|
|
// Recursively transform the users of PHI nodes. This will lazily create the
|
|
|
|
// PHIs that are needed for individual elements. Keep track of what PHIs we
|
|
|
|
// see in InsertedScalarizedValues so that we don't get infinite loops (very
|
|
|
|
// antisocial). If the PHI is already in InsertedScalarizedValues, it has
|
|
|
|
// already been seen first by another load, so its uses have already been
|
|
|
|
// processed.
|
2007-09-14 02:00:31 +08:00
|
|
|
PHINode *PN = cast<PHINode>(LoadUser);
|
2011-07-21 14:21:31 +08:00
|
|
|
if (!InsertedScalarizedValues.insert(std::make_pair(PN,
|
|
|
|
std::vector<Value*>())).second)
|
|
|
|
return;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
// If this is the first time we've seen this PHI, recursively process all
|
|
|
|
// users.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
|
2008-12-17 13:42:08 +08:00
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2009-11-06 12:27:31 +08:00
|
|
|
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
|
2008-12-17 13:42:08 +08:00
|
|
|
}
|
2007-09-14 01:29:05 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// We are performing Heap SRoA on a global. Ptr is a value loaded from the
|
|
|
|
/// global. Eliminate all uses of Ptr, making them use FieldGlobals instead.
|
|
|
|
/// All uses of loaded values satisfy AllGlobalLoadUsesSimpleEnoughForHeapSRA.
|
2010-10-19 05:16:00 +08:00
|
|
|
static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
|
2008-12-17 13:28:49 +08:00
|
|
|
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
|
2009-11-06 12:27:31 +08:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (auto UI = Load->user_begin(), E = Load->user_end(); UI != E;) {
|
2008-12-17 13:42:08 +08:00
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2009-11-06 12:27:31 +08:00
|
|
|
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
|
2008-12-17 13:42:08 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2008-12-17 13:28:49 +08:00
|
|
|
if (Load->use_empty()) {
|
|
|
|
Load->eraseFromParent();
|
|
|
|
InsertedScalarizedValues.erase(Load);
|
|
|
|
}
|
2006-10-01 07:32:09 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// CI is an allocation of an array of structures. Break it up into multiple
|
|
|
|
/// allocations of arrays of the fields.
|
2009-11-07 08:16:28 +08:00
|
|
|
static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
|
2015-03-05 02:43:29 +08:00
|
|
|
Value *NElems, const DataLayout &DL,
|
2012-08-29 23:32:21 +08:00
|
|
|
const TargetLibraryInfo *TLI) {
|
2010-01-05 09:28:05 +08:00
|
|
|
DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
|
2012-08-29 23:32:21 +08:00
|
|
|
Type *MAT = getMallocAllocatedType(CI, TLI);
|
2011-07-18 12:54:35 +08:00
|
|
|
StructType *STy = cast<StructType>(MAT);
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
// There is guaranteed to be at least one use of the malloc (storing
|
|
|
|
// it into GV). If there are other uses, change them to be uses of
|
|
|
|
// the global to simplify later code. This also deletes the store
|
|
|
|
// into GV.
|
2009-11-07 08:16:28 +08:00
|
|
|
ReplaceUsesOfMallocWithGlobal(CI, GV);
|
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Okay, at this point, there are no users of the malloc. Insert N
|
|
|
|
// new mallocs at the same place as CI, and N globals.
|
|
|
|
std::vector<Value*> FieldGlobals;
|
|
|
|
std::vector<Value*> FieldMallocs;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-04-24 04:36:10 +08:00
|
|
|
unsigned AS = GV->getType()->getPointerAddressSpace();
|
2009-09-19 06:35:49 +08:00
|
|
|
for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *FieldTy = STy->getElementType(FieldNo);
|
2014-04-24 04:36:10 +08:00
|
|
|
PointerType *PFieldTy = PointerType::get(FieldTy, AS);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-12-23 03:16:50 +08:00
|
|
|
GlobalVariable *NGV = new GlobalVariable(
|
|
|
|
*GV->getParent(), PFieldTy, false, GlobalValue::InternalLinkage,
|
|
|
|
Constant::getNullValue(PFieldTy), GV->getName() + ".f" + Twine(FieldNo),
|
|
|
|
nullptr, GV->getThreadLocalMode());
|
2009-09-19 06:35:49 +08:00
|
|
|
FieldGlobals.push_back(NGV);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-03-05 02:43:29 +08:00
|
|
|
unsigned TypeSize = DL.getTypeAllocSize(FieldTy);
|
2011-07-18 12:54:35 +08:00
|
|
|
if (StructType *ST = dyn_cast<StructType>(FieldTy))
|
2015-03-05 02:43:29 +08:00
|
|
|
TypeSize = DL.getStructLayout(ST)->getSizeInBytes();
|
|
|
|
Type *IntPtrTy = DL.getIntPtrType(CI->getType());
|
2009-11-07 08:16:28 +08:00
|
|
|
Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
|
|
|
|
ConstantInt::get(IntPtrTy, TypeSize),
|
2014-04-25 13:29:35 +08:00
|
|
|
NElems, nullptr,
|
2009-11-07 08:16:28 +08:00
|
|
|
CI->getName() + ".f" + Twine(FieldNo));
|
2010-02-27 02:23:13 +08:00
|
|
|
FieldMallocs.push_back(NMI);
|
2009-11-07 08:16:28 +08:00
|
|
|
new StoreInst(NMI, NGV, CI);
|
2009-09-19 06:35:49 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// The tricky aspect of this transformation is handling the case when malloc
|
|
|
|
// fails. In the original code, malloc failing would set the result pointer
|
|
|
|
// of malloc to null. In this case, some mallocs could succeed and others
|
|
|
|
// could fail. As such, we emit code that looks like this:
|
|
|
|
// F0 = malloc(field0)
|
|
|
|
// F1 = malloc(field1)
|
|
|
|
// F2 = malloc(field2)
|
|
|
|
// if (F0 == 0 || F1 == 0 || F2 == 0) {
|
|
|
|
// if (F0) { free(F0); F0 = 0; }
|
|
|
|
// if (F1) { free(F1); F1 = 0; }
|
|
|
|
// if (F2) { free(F2); F2 = 0; }
|
|
|
|
// }
|
2009-11-10 16:32:25 +08:00
|
|
|
// The malloc can also fail if its argument is too large.
|
2010-06-24 22:42:01 +08:00
|
|
|
Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0);
|
|
|
|
Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0),
|
2009-11-10 16:32:25 +08:00
|
|
|
ConstantZero, "isneg");
|
2009-09-19 06:35:49 +08:00
|
|
|
for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
|
2009-11-07 08:16:28 +08:00
|
|
|
Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
|
|
|
|
Constant::getNullValue(FieldMallocs[i]->getType()),
|
|
|
|
"isnull");
|
2009-11-10 16:32:25 +08:00
|
|
|
RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
|
2009-09-19 06:35:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split the basic block at the old malloc.
|
2009-11-07 08:16:28 +08:00
|
|
|
BasicBlock *OrigBB = CI->getParent();
|
2015-10-14 01:51:03 +08:00
|
|
|
BasicBlock *ContBB =
|
|
|
|
OrigBB->splitBasicBlock(CI->getIterator(), "malloc_cont");
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Create the block to check the first condition. Put all these blocks at the
|
|
|
|
// end of the function as they are unlikely to be executed.
|
2009-11-06 12:27:31 +08:00
|
|
|
BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
|
|
|
|
"malloc_ret_null",
|
2009-09-19 06:35:49 +08:00
|
|
|
OrigBB->getParent());
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Remove the uncond branch from OrigBB to ContBB, turning it into a cond
|
|
|
|
// branch on RunningOr.
|
|
|
|
OrigBB->getTerminator()->eraseFromParent();
|
|
|
|
BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Within the NullPtrBlock, we need to emit a comparison and branch for each
|
|
|
|
// pointer, because some may be null while others are not.
|
|
|
|
for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
|
|
|
|
Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
|
2010-10-19 05:16:00 +08:00
|
|
|
Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
|
2011-09-28 04:39:19 +08:00
|
|
|
Constant::getNullValue(GVVal->getType()));
|
2009-11-06 12:27:31 +08:00
|
|
|
BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
|
2009-09-19 06:35:49 +08:00
|
|
|
OrigBB->getParent());
|
2009-11-06 12:27:31 +08:00
|
|
|
BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
|
2009-09-19 06:35:49 +08:00
|
|
|
OrigBB->getParent());
|
2009-10-24 12:23:03 +08:00
|
|
|
Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
|
|
|
|
Cmp, NullPtrBlock);
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
// Fill in FreeBlock.
|
2009-10-24 12:23:03 +08:00
|
|
|
CallInst::CreateFree(GVVal, BI);
|
2009-09-19 06:35:49 +08:00
|
|
|
new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
|
|
|
|
FreeBlock);
|
|
|
|
BranchInst::Create(NextBlock, FreeBlock);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
NullPtrBlock = NextBlock;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
BranchInst::Create(ContBB, NullPtrBlock);
|
2009-11-07 08:16:28 +08:00
|
|
|
|
|
|
|
// CI is no longer needed, remove it.
|
2009-09-19 06:35:49 +08:00
|
|
|
CI->eraseFromParent();
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// As we process loads, if we can't immediately update all uses of the load,
|
|
|
|
/// keep track of what scalarized loads are inserted for a given load.
|
2009-09-19 06:35:49 +08:00
|
|
|
DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
|
|
|
|
InsertedScalarizedValues[GV] = FieldGlobals;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Okay, the malloc site is completely handled. All of the uses of GV are now
|
|
|
|
// loads, and all uses of those loads are simple. Rewrite them to use loads
|
|
|
|
// of the per-field globals instead.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (auto UI = GV->user_begin(), E = GV->user_end(); UI != E;) {
|
2009-09-19 06:35:49 +08:00
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
|
2009-11-06 12:27:31 +08:00
|
|
|
RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
|
2009-09-19 06:35:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Must be a store of null.
|
|
|
|
StoreInst *SI = cast<StoreInst>(User);
|
|
|
|
assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
|
|
|
|
"Unexpected heap-sra user!");
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Insert a store of null into each global.
|
|
|
|
for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
|
2016-01-17 04:30:46 +08:00
|
|
|
Type *ValTy = cast<GlobalValue>(FieldGlobals[i])->getValueType();
|
|
|
|
Constant *Null = Constant::getNullValue(ValTy);
|
2009-09-19 06:35:49 +08:00
|
|
|
new StoreInst(Null, FieldGlobals[i], SI);
|
|
|
|
}
|
|
|
|
// Erase the original store.
|
|
|
|
SI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// While we have PHIs that are interesting to rewrite, do it.
|
|
|
|
while (!PHIsToRewrite.empty()) {
|
|
|
|
PHINode *PN = PHIsToRewrite.back().first;
|
|
|
|
unsigned FieldNo = PHIsToRewrite.back().second;
|
|
|
|
PHIsToRewrite.pop_back();
|
|
|
|
PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]);
|
|
|
|
assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
|
|
|
|
|
|
|
|
// Add all the incoming values. This can materialize more phis.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *InVal = PN->getIncomingValue(i);
|
|
|
|
InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
|
2009-11-06 12:27:31 +08:00
|
|
|
PHIsToRewrite);
|
2009-09-19 06:35:49 +08:00
|
|
|
FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Drop all inter-phi links and any loads that made it this far.
|
|
|
|
for (DenseMap<Value*, std::vector<Value*> >::iterator
|
|
|
|
I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(I->first))
|
|
|
|
PN->dropAllReferences();
|
|
|
|
else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
|
|
|
|
LI->dropAllReferences();
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// Delete all the phis and loads now that inter-references are dead.
|
|
|
|
for (DenseMap<Value*, std::vector<Value*> >::iterator
|
|
|
|
I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(I->first))
|
|
|
|
PN->eraseFromParent();
|
|
|
|
else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
|
|
|
|
LI->eraseFromParent();
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
// The old global is now dead, remove it.
|
|
|
|
GV->eraseFromParent();
|
|
|
|
|
|
|
|
++NumHeapSRA;
|
|
|
|
return cast<GlobalVariable>(FieldGlobals[0]);
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// This function is called when we see a pointer global variable with a single
|
|
|
|
/// value stored it that is a malloc or cast of malloc.
|
2015-12-23 03:16:50 +08:00
|
|
|
static bool tryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, CallInst *CI,
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *AllocTy,
|
2012-02-06 03:56:38 +08:00
|
|
|
AtomicOrdering Ordering,
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL,
|
2012-02-12 09:13:18 +08:00
|
|
|
TargetLibraryInfo *TLI) {
|
2009-09-19 06:35:49 +08:00
|
|
|
// If this is a malloc of an abstract type, don't touch it.
|
|
|
|
if (!AllocTy->isSized())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can't optimize this global unless all uses of it are *known* to be
|
|
|
|
// of the malloc value, not of the null initializer value (consider a use
|
|
|
|
// that compares the global's value against zero to see if the malloc has
|
|
|
|
// been reached). To do this, we check to see if all uses of the global
|
|
|
|
// would trap if the global were null: this proves that they must all
|
|
|
|
// happen after the malloc.
|
|
|
|
if (!AllUsesOfLoadedValueWillTrapIfNull(GV))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can't optimize this if the malloc itself is used in a complex way,
|
|
|
|
// for example, being stored into multiple globals. This allows the
|
2012-02-06 03:48:37 +08:00
|
|
|
// malloc to be stored into the specified global, loaded icmp'd, and
|
2009-09-19 06:35:49 +08:00
|
|
|
// GEP'd. These are all things we could transform to using the global
|
|
|
|
// for.
|
2010-04-15 04:52:55 +08:00
|
|
|
SmallPtrSet<const PHINode*, 8> PHIs;
|
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
|
|
|
|
return false;
|
2009-09-19 06:35:49 +08:00
|
|
|
|
|
|
|
// If we have a global that is only initialized with a fixed size malloc,
|
|
|
|
// transform the program to use global memory instead of malloc'd memory.
|
|
|
|
// This eliminates dynamic allocation, avoids an indirection accessing the
|
|
|
|
// data, and exposes the resultant global to further GlobalOpt.
|
2009-10-17 07:12:25 +08:00
|
|
|
// We cannot optimize the malloc if we cannot determine malloc array size.
|
2015-03-10 10:37:25 +08:00
|
|
|
Value *NElems = getMallocArraySize(CI, DL, TLI, true);
|
2010-04-15 04:52:55 +08:00
|
|
|
if (!NElems)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
|
|
|
|
// Restrict this transformation to only working on small allocations
|
|
|
|
// (2048 bytes currently), as we don't want to introduce a 16M global or
|
|
|
|
// something.
|
2015-03-05 02:43:29 +08:00
|
|
|
if (NElements->getZExtValue() * DL.getTypeAllocSize(AllocTy) < 2048) {
|
2015-12-23 03:16:50 +08:00
|
|
|
OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, DL, TLI);
|
2010-04-15 04:52:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-04-15 04:52:55 +08:00
|
|
|
// If the allocation is an array of structures, consider transforming this
|
|
|
|
// into multiple malloc'd arrays, one for each field. This is basically
|
|
|
|
// SRoA for malloc'd memory.
|
|
|
|
|
2012-02-06 03:56:38 +08:00
|
|
|
if (Ordering != NotAtomic)
|
|
|
|
return false;
|
|
|
|
|
2010-04-15 04:52:55 +08:00
|
|
|
// If this is an allocation of a fixed size array of structs, analyze as a
|
|
|
|
// variable size array. malloc [100 x struct],1 -> malloc struct, 100
|
2010-06-24 22:42:01 +08:00
|
|
|
if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
|
2011-07-18 12:54:35 +08:00
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
|
2010-04-15 04:52:55 +08:00
|
|
|
AllocTy = AT->getElementType();
|
2010-06-24 22:42:01 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
StructType *AllocSTy = dyn_cast<StructType>(AllocTy);
|
2010-04-15 04:52:55 +08:00
|
|
|
if (!AllocSTy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This the structure has an unreasonable number of fields, leave it
|
|
|
|
// alone.
|
|
|
|
if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
|
|
|
|
AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
|
|
|
|
|
|
|
|
// If this is a fixed size array, transform the Malloc to be an alloc of
|
|
|
|
// structs. malloc [100 x struct],1 -> malloc struct, 100
|
2012-08-29 23:32:21 +08:00
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) {
|
2015-03-05 02:43:29 +08:00
|
|
|
Type *IntPtrTy = DL.getIntPtrType(CI->getType());
|
|
|
|
unsigned TypeSize = DL.getStructLayout(AllocSTy)->getSizeInBytes();
|
2010-04-15 04:52:55 +08:00
|
|
|
Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
|
|
|
|
Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
|
|
|
|
Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
|
|
|
|
AllocSize, NumElements,
|
2014-04-25 13:29:35 +08:00
|
|
|
nullptr, CI->getName());
|
2010-04-15 04:52:55 +08:00
|
|
|
Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
|
|
|
|
CI->replaceAllUsesWith(Cast);
|
|
|
|
CI->eraseFromParent();
|
2012-06-22 08:25:01 +08:00
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc))
|
|
|
|
CI = cast<CallInst>(BCI->getOperand(0));
|
|
|
|
else
|
2012-06-22 08:29:58 +08:00
|
|
|
CI = cast<CallInst>(Malloc);
|
2009-09-19 06:35:49 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-12-23 03:16:50 +08:00
|
|
|
PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, DL, TLI, true), DL,
|
|
|
|
TLI);
|
2010-04-15 04:52:55 +08:00
|
|
|
return true;
|
2009-09-19 06:35:49 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2009-09-19 06:35:49 +08:00
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
}
|
2009-09-19 06:35:49 +08:00
|
|
|
|
2015-12-23 03:16:50 +08:00
|
|
|
// Try to optimize globals based on the knowledge that only one value (besides
|
|
|
|
// its initializer) is ever stored to the global.
|
|
|
|
static bool optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
|
2012-02-06 03:56:38 +08:00
|
|
|
AtomicOrdering Ordering,
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL,
|
2014-02-25 07:12:18 +08:00
|
|
|
TargetLibraryInfo *TLI) {
|
2008-12-16 05:20:32 +08:00
|
|
|
// Ignore no-op GEPs and bitcasts.
|
|
|
|
StoredOnceVal = StoredOnceVal->stripPointerCasts();
|
2004-10-10 05:48:45 +08:00
|
|
|
|
2004-10-11 07:14:11 +08:00
|
|
|
// If we are dealing with a pointer global that is initialized to null and
|
|
|
|
// only has one (non-null) value stored into it, then we can optimize any
|
|
|
|
// users of the loaded value (often calls and loads) that would trap if the
|
|
|
|
// value was null.
|
2010-02-16 19:11:14 +08:00
|
|
|
if (GV->getInitializer()->getType()->isPointerTy() &&
|
2004-10-10 05:48:45 +08:00
|
|
|
GV->getInitializer()->isNullValue()) {
|
2004-10-11 07:14:11 +08:00
|
|
|
if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
|
|
|
|
if (GV->getInitializer()->getType() != SOVC->getType())
|
2011-05-22 15:15:13 +08:00
|
|
|
SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-10-11 07:14:11 +08:00
|
|
|
// Optimize away any trapping uses of the loaded value.
|
2014-02-21 08:06:31 +08:00
|
|
|
if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, TLI))
|
2004-10-11 01:07:12 +08:00
|
|
|
return true;
|
2012-08-29 23:32:21 +08:00
|
|
|
} else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) {
|
|
|
|
Type *MallocType = getMallocAllocatedType(CI, TLI);
|
2015-12-23 03:16:50 +08:00
|
|
|
if (MallocType && tryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType,
|
|
|
|
Ordering, DL, TLI))
|
2009-11-07 08:16:28 +08:00
|
|
|
return true;
|
2004-10-11 07:14:11 +08:00
|
|
|
}
|
2004-10-10 05:48:45 +08:00
|
|
|
}
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
llvm-svn: 16916
2004-10-11 13:54:41 +08:00
|
|
|
|
2004-10-10 05:48:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
2004-10-09 04:59:28 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// At this point, we have learned that the only two values ever stored into GV
|
|
|
|
/// are its initializer and OtherVal. See if we can shrink the global into a
|
|
|
|
/// boolean and select between the two values whenever it is used. This exposes
|
|
|
|
/// the values to other scalar optimizations.
|
2014-03-23 12:22:31 +08:00
|
|
|
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
|
2016-01-17 04:30:46 +08:00
|
|
|
Type *GVElType = GV->getValueType();
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-03-23 12:22:31 +08:00
|
|
|
// If GVElType is already i1, it is already shrunk. If the type of the GV is
|
|
|
|
// an FP value, pointer or vector, don't do this optimization because a select
|
|
|
|
// between them is very expensive and unlikely to lead to later
|
|
|
|
// simplification. In these cases, we typically end up with "cond ? v1 : v2"
|
|
|
|
// where v1 and v2 both require constant pool loads, a big loss.
|
2009-11-06 12:27:31 +08:00
|
|
|
if (GVElType == Type::getInt1Ty(GV->getContext()) ||
|
2010-02-16 00:12:20 +08:00
|
|
|
GVElType->isFloatingPointTy() ||
|
2010-02-16 19:11:14 +08:00
|
|
|
GVElType->isPointerTy() || GVElType->isVectorTy())
|
2008-01-14 09:17:44 +08:00
|
|
|
return false;
|
2010-07-12 22:13:15 +08:00
|
|
|
|
2008-01-14 09:17:44 +08:00
|
|
|
// Walk the use list of the global seeing if all the uses are load or store.
|
|
|
|
// If there is anything else, bail out.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : GV->users())
|
2010-07-12 22:13:15 +08:00
|
|
|
if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
|
2008-01-14 09:17:44 +08:00
|
|
|
return false;
|
2010-07-12 22:13:15 +08:00
|
|
|
|
2015-10-28 22:30:53 +08:00
|
|
|
DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV << "\n");
|
2014-03-23 12:22:31 +08:00
|
|
|
|
|
|
|
// Create the new global, initializing it to false.
|
|
|
|
GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
|
|
|
|
false,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
ConstantInt::getFalse(GV->getContext()),
|
|
|
|
GV->getName()+".b",
|
|
|
|
GV->getThreadLocalMode(),
|
|
|
|
GV->getType()->getAddressSpace());
|
2015-10-14 01:51:03 +08:00
|
|
|
GV->getParent()->getGlobalList().insert(GV->getIterator(), NewGV);
|
2014-03-23 12:22:31 +08:00
|
|
|
|
2004-12-12 13:53:50 +08:00
|
|
|
Constant *InitVal = GV->getInitializer();
|
2009-11-06 12:27:31 +08:00
|
|
|
assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
|
2014-03-23 12:22:31 +08:00
|
|
|
"No reason to shrink to bool!");
|
2004-12-12 13:53:50 +08:00
|
|
|
|
2014-03-23 12:22:31 +08:00
|
|
|
// If initialized to zero and storing one into the global, we can use a cast
|
|
|
|
// instead of a select to synthesize the desired value.
|
|
|
|
bool IsOneZero = false;
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
|
|
|
|
IsOneZero = InitVal->isNullValue() && CI->isOne();
|
2004-12-12 13:53:50 +08:00
|
|
|
|
2014-03-23 12:22:31 +08:00
|
|
|
while (!GV->use_empty()) {
|
|
|
|
Instruction *UI = cast<Instruction>(GV->user_back());
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
|
|
|
|
// Change the store into a boolean store.
|
|
|
|
bool StoringOther = SI->getOperand(0) == OtherVal;
|
|
|
|
// Only do this if we weren't storing a loaded value.
|
|
|
|
Value *StoreVal;
|
|
|
|
if (StoringOther || SI->getOperand(0) == InitVal) {
|
|
|
|
StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
|
|
|
|
StoringOther);
|
2013-02-14 07:00:51 +08:00
|
|
|
} else {
|
2014-03-23 12:22:31 +08:00
|
|
|
// Otherwise, we are storing a previously loaded copy. To do this,
|
|
|
|
// change the copy from copying the original value to just copying the
|
|
|
|
// bool.
|
|
|
|
Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
|
|
|
|
|
|
|
|
// If we've already replaced the input, StoredVal will be a cast or
|
|
|
|
// select instruction. If not, it will be a load of the original
|
|
|
|
// global.
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
|
|
|
|
assert(LI->getOperand(0) == GV && "Not a copy!");
|
|
|
|
// Insert a new load, to preserve the saved value.
|
|
|
|
StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0,
|
|
|
|
LI->getOrdering(), LI->getSynchScope(), LI);
|
|
|
|
} else {
|
|
|
|
assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
|
|
|
|
"This is not a form that we understand!");
|
|
|
|
StoreVal = StoredVal->getOperand(0);
|
|
|
|
assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
|
|
|
|
}
|
2004-12-13 03:34:41 +08:00
|
|
|
}
|
2014-03-23 12:22:31 +08:00
|
|
|
new StoreInst(StoreVal, NewGV, false, 0,
|
|
|
|
SI->getOrdering(), SI->getSynchScope(), SI);
|
|
|
|
} else {
|
|
|
|
// Change the load into a load of bool then a select.
|
|
|
|
LoadInst *LI = cast<LoadInst>(UI);
|
|
|
|
LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0,
|
|
|
|
LI->getOrdering(), LI->getSynchScope(), LI);
|
|
|
|
Value *NSI;
|
|
|
|
if (IsOneZero)
|
|
|
|
NSI = new ZExtInst(NLI, LI->getType(), "", LI);
|
|
|
|
else
|
|
|
|
NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
|
|
|
|
NSI->takeName(LI);
|
|
|
|
LI->replaceAllUsesWith(NSI);
|
2009-03-06 09:39:36 +08:00
|
|
|
}
|
2014-03-23 12:22:31 +08:00
|
|
|
UI->eraseFromParent();
|
2004-12-12 13:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-03-23 12:22:31 +08:00
|
|
|
// Retain the name of the old global variable. People who are debugging their
|
|
|
|
// programs may expect these variables to be named the same.
|
|
|
|
NewGV->takeName(GV);
|
|
|
|
GV->eraseFromParent();
|
2008-01-14 09:17:44 +08:00
|
|
|
return true;
|
2004-12-12 13:53:50 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 03:38:07 +08:00
|
|
|
bool GlobalOpt::deleteIfDead(GlobalValue &GV) {
|
|
|
|
GV.removeDeadConstantUsers();
|
|
|
|
|
|
|
|
if (!GV.isDiscardableIfUnused())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const Comdat *C = GV.getComdat())
|
|
|
|
if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Dead;
|
|
|
|
if (auto *F = dyn_cast<Function>(&GV))
|
|
|
|
Dead = F->isDefTriviallyDead();
|
|
|
|
else
|
|
|
|
Dead = GV.use_empty();
|
|
|
|
if (!Dead)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
|
|
|
|
GV.eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
return true;
|
|
|
|
}
|
2004-12-12 13:53:50 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Analyze the specified global variable and optimize it if possible. If we
|
|
|
|
/// make a change, return true.
|
2015-12-23 04:43:30 +08:00
|
|
|
bool GlobalOpt::processGlobal(GlobalValue &GV) {
|
2015-12-23 03:38:07 +08:00
|
|
|
// Do more involved optimizations if the global is internal.
|
2015-12-23 04:43:30 +08:00
|
|
|
if (!GV.hasLocalLinkage())
|
2012-06-16 02:00:24 +08:00
|
|
|
return false;
|
|
|
|
|
2011-01-20 00:32:21 +08:00
|
|
|
GlobalStatus GS;
|
|
|
|
|
2015-12-23 04:43:30 +08:00
|
|
|
if (GlobalStatus::analyzeGlobal(&GV, GS))
|
2011-01-18 12:36:06 +08:00
|
|
|
return false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-12-23 04:43:30 +08:00
|
|
|
bool Changed = false;
|
|
|
|
if (!GS.IsCompared && !GV.hasUnnamedAddr()) {
|
|
|
|
GV.setUnnamedAddr(true);
|
2011-01-20 00:32:21 +08:00
|
|
|
NumUnnamed++;
|
2015-12-23 04:43:30 +08:00
|
|
|
Changed = true;
|
2011-01-20 00:32:21 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 04:43:30 +08:00
|
|
|
auto *GVar = dyn_cast<GlobalVariable>(&GV);
|
|
|
|
if (!GVar)
|
|
|
|
return Changed;
|
|
|
|
|
|
|
|
if (GVar->isConstant() || !GVar->hasInitializer())
|
|
|
|
return Changed;
|
2011-01-20 00:32:21 +08:00
|
|
|
|
2015-12-23 04:43:30 +08:00
|
|
|
return processInternalGlobal(GVar, GS) || Changed;
|
2011-01-20 00:32:21 +08:00
|
|
|
}
|
|
|
|
|
2015-11-15 22:21:37 +08:00
|
|
|
bool GlobalOpt::isPointerValueDeadOnEntryToFunction(const Function *F, GlobalValue *GV) {
|
|
|
|
// Find all uses of GV. We expect them all to be in F, and if we can't
|
|
|
|
// identify any of the uses we bail out.
|
|
|
|
//
|
|
|
|
// On each of these uses, identify if the memory that GV points to is
|
|
|
|
// used/required/live at the start of the function. If it is not, for example
|
|
|
|
// if the first thing the function does is store to the GV, the GV can
|
|
|
|
// possibly be demoted.
|
|
|
|
//
|
|
|
|
// We don't do an exhaustive search for memory operations - simply look
|
|
|
|
// through bitcasts as they're quite common and benign.
|
|
|
|
const DataLayout &DL = GV->getParent()->getDataLayout();
|
|
|
|
SmallVector<LoadInst *, 4> Loads;
|
|
|
|
SmallVector<StoreInst *, 4> Stores;
|
|
|
|
for (auto *U : GV->users()) {
|
|
|
|
if (Operator::getOpcode(U) == Instruction::BitCast) {
|
|
|
|
for (auto *UU : U->users()) {
|
|
|
|
if (auto *LI = dyn_cast<LoadInst>(UU))
|
|
|
|
Loads.push_back(LI);
|
|
|
|
else if (auto *SI = dyn_cast<StoreInst>(UU))
|
|
|
|
Stores.push_back(SI);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *I = dyn_cast<Instruction>(U);
|
|
|
|
if (!I)
|
|
|
|
return false;
|
|
|
|
assert(I->getParent()->getParent() == F);
|
|
|
|
|
|
|
|
if (auto *LI = dyn_cast<LoadInst>(I))
|
|
|
|
Loads.push_back(LI);
|
|
|
|
else if (auto *SI = dyn_cast<StoreInst>(I))
|
|
|
|
Stores.push_back(SI);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have identified all uses of GV into loads and stores. Now check if all
|
|
|
|
// of them are known not to depend on the value of the global at the function
|
|
|
|
// entry point. We do this by ensuring that every load is dominated by at
|
|
|
|
// least one store.
|
|
|
|
auto &DT = getAnalysis<DominatorTreeWrapperPass>(*const_cast<Function *>(F))
|
|
|
|
.getDomTree();
|
|
|
|
|
2015-11-16 18:16:22 +08:00
|
|
|
// The below check is quadratic. Check we're not going to do too many tests.
|
|
|
|
// FIXME: Even though this will always have worst-case quadratic time, we
|
|
|
|
// could put effort into minimizing the average time by putting stores that
|
|
|
|
// have been shown to dominate at least one load at the beginning of the
|
|
|
|
// Stores array, making subsequent dominance checks more likely to succeed
|
|
|
|
// early.
|
|
|
|
//
|
|
|
|
// The threshold here is fairly large because global->local demotion is a
|
|
|
|
// very powerful optimization should it fire.
|
|
|
|
const unsigned Threshold = 100;
|
|
|
|
if (Loads.size() * Stores.size() > Threshold)
|
|
|
|
return false;
|
|
|
|
|
2015-11-15 22:21:37 +08:00
|
|
|
for (auto *L : Loads) {
|
|
|
|
auto *LTy = L->getType();
|
|
|
|
if (!std::any_of(Stores.begin(), Stores.end(), [&](StoreInst *S) {
|
|
|
|
auto *STy = S->getValueOperand()->getType();
|
|
|
|
// The load is only dominated by the store if DomTree says so
|
|
|
|
// and the number of bits loaded in L is less than or equal to
|
|
|
|
// the number of bits stored in S.
|
|
|
|
return DT.dominates(S, L) &&
|
|
|
|
DL.getTypeStoreSize(LTy) <= DL.getTypeStoreSize(STy);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// All loads have known dependences inside F, so the global can be localized.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-20 02:04:33 +08:00
|
|
|
/// C may have non-instruction users. Can all of those users be turned into
|
|
|
|
/// instructions?
|
|
|
|
static bool allNonInstructionUsersCanBeMadeInstructions(Constant *C) {
|
|
|
|
// We don't do this exhaustively. The most common pattern that we really need
|
|
|
|
// to care about is a constant GEP or constant bitcast - so just looking
|
|
|
|
// through one single ConstantExpr.
|
|
|
|
//
|
|
|
|
// The set of constants that this function returns true for must be able to be
|
|
|
|
// handled by makeAllConstantUsesInstructions.
|
|
|
|
for (auto *U : C->users()) {
|
|
|
|
if (isa<Instruction>(U))
|
|
|
|
continue;
|
|
|
|
if (!isa<ConstantExpr>(U))
|
|
|
|
// Non instruction, non-constantexpr user; cannot convert this.
|
|
|
|
return false;
|
|
|
|
for (auto *UU : U->users())
|
|
|
|
if (!isa<Instruction>(UU))
|
|
|
|
// A constantexpr used by another constant. We don't try and recurse any
|
|
|
|
// further but just bail out at this point.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// C may have non-instruction users, and
|
|
|
|
/// allNonInstructionUsersCanBeMadeInstructions has returned true. Convert the
|
|
|
|
/// non-instruction users to instructions.
|
|
|
|
static void makeAllConstantUsesInstructions(Constant *C) {
|
|
|
|
SmallVector<ConstantExpr*,4> Users;
|
|
|
|
for (auto *U : C->users()) {
|
|
|
|
if (isa<ConstantExpr>(U))
|
|
|
|
Users.push_back(cast<ConstantExpr>(U));
|
|
|
|
else
|
|
|
|
// We should never get here; allNonInstructionUsersCanBeMadeInstructions
|
|
|
|
// should not have returned true for C.
|
|
|
|
assert(
|
|
|
|
isa<Instruction>(U) &&
|
|
|
|
"Can't transform non-constantexpr non-instruction to instruction!");
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<Value*,4> UUsers;
|
|
|
|
for (auto *U : Users) {
|
|
|
|
UUsers.clear();
|
|
|
|
for (auto *UU : U->users())
|
|
|
|
UUsers.push_back(UU);
|
|
|
|
for (auto *UU : UUsers) {
|
|
|
|
Instruction *UI = cast<Instruction>(UU);
|
|
|
|
Instruction *NewU = U->getAsInstruction();
|
|
|
|
NewU->insertBefore(UI);
|
|
|
|
UI->replaceUsesOfWith(U, NewU);
|
|
|
|
}
|
|
|
|
U->dropAllReferences();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Analyze the specified global variable and optimize
|
2011-01-20 00:32:21 +08:00
|
|
|
/// it if possible. If we make a change, return true.
|
2015-12-23 03:16:50 +08:00
|
|
|
bool GlobalOpt::processInternalGlobal(GlobalVariable *GV,
|
2011-01-20 00:32:21 +08:00
|
|
|
const GlobalStatus &GS) {
|
2015-03-05 02:43:29 +08:00
|
|
|
auto &DL = GV->getParent()->getDataLayout();
|
2015-11-15 22:21:37 +08:00
|
|
|
// If this is a first class global and has only one accessing function and
|
|
|
|
// this function is non-recursive, we replace the global with a local alloca
|
|
|
|
// in this function.
|
2013-10-08 03:03:24 +08:00
|
|
|
//
|
2013-12-05 13:44:44 +08:00
|
|
|
// NOTE: It doesn't make sense to promote non-single-value types since we
|
2013-10-08 03:03:24 +08:00
|
|
|
// are just replacing static memory to stack memory.
|
|
|
|
//
|
|
|
|
// If the global is in different address space, don't bring it to stack.
|
|
|
|
if (!GS.HasMultipleAccessingFunctions &&
|
2015-11-20 02:04:33 +08:00
|
|
|
GS.AccessingFunction &&
|
2016-01-17 04:30:46 +08:00
|
|
|
GV->getValueType()->isSingleValueType() &&
|
2015-11-15 22:21:37 +08:00
|
|
|
GV->getType()->getAddressSpace() == 0 &&
|
|
|
|
!GV->isExternallyInitialized() &&
|
2015-11-20 02:04:33 +08:00
|
|
|
allNonInstructionUsersCanBeMadeInstructions(GV) &&
|
2015-11-15 22:21:37 +08:00
|
|
|
GS.AccessingFunction->doesNotRecurse() &&
|
|
|
|
isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV) ) {
|
2015-11-13 19:05:13 +08:00
|
|
|
DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
|
2013-10-08 03:03:24 +08:00
|
|
|
Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
|
|
|
|
->getEntryBlock().begin());
|
2016-01-17 04:30:46 +08:00
|
|
|
Type *ElemTy = GV->getValueType();
|
2013-10-08 03:03:24 +08:00
|
|
|
// FIXME: Pass Global's alignment when globals have alignment
|
2014-04-25 13:29:35 +08:00
|
|
|
AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
|
|
|
|
GV->getName(), &FirstI);
|
2013-10-08 03:03:24 +08:00
|
|
|
if (!isa<UndefValue>(GV->getInitializer()))
|
|
|
|
new StoreInst(GV->getInitializer(), Alloca, &FirstI);
|
|
|
|
|
2015-11-20 02:04:33 +08:00
|
|
|
makeAllConstantUsesInstructions(GV);
|
|
|
|
|
2013-10-08 03:03:24 +08:00
|
|
|
GV->replaceAllUsesWith(Alloca);
|
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumLocalized;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-18 12:36:06 +08:00
|
|
|
// If the global is never loaded (but may be stored to), it is dead.
|
|
|
|
// Delete it now.
|
2013-10-18 02:18:52 +08:00
|
|
|
if (!GS.IsLoaded) {
|
2015-11-13 19:05:13 +08:00
|
|
|
DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n");
|
2004-10-09 11:32:52 +08:00
|
|
|
|
2012-07-24 15:21:08 +08:00
|
|
|
bool Changed;
|
|
|
|
if (isLeakCheckerRoot(GV)) {
|
|
|
|
// Delete any constant stores to the global.
|
2012-08-29 23:32:21 +08:00
|
|
|
Changed = CleanupPointerRootUsers(GV, TLI);
|
2012-07-24 15:21:08 +08:00
|
|
|
} else {
|
|
|
|
// Delete any stores we can find to the global. We may not be able to
|
|
|
|
// make it completely dead though.
|
2014-02-21 08:06:31 +08:00
|
|
|
Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
|
2012-07-24 15:21:08 +08:00
|
|
|
}
|
2011-01-18 12:36:06 +08:00
|
|
|
|
|
|
|
// If the global is dead now, delete it.
|
|
|
|
if (GV->use_empty()) {
|
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
|
2013-10-18 02:18:52 +08:00
|
|
|
} else if (GS.StoredType <= GlobalStatus::InitializerStored) {
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
|
2011-01-18 12:36:06 +08:00
|
|
|
GV->setConstant(true);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2011-01-18 12:36:06 +08:00
|
|
|
// Clean up any obviously simplifiable users now.
|
2014-02-21 08:06:31 +08:00
|
|
|
CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2011-01-18 12:36:06 +08:00
|
|
|
// If the global is dead now, just nuke it.
|
|
|
|
if (GV->use_empty()) {
|
|
|
|
DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
|
|
|
|
<< "all users and delete global!\n");
|
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2011-01-18 12:36:06 +08:00
|
|
|
++NumMarked;
|
|
|
|
return true;
|
|
|
|
} else if (!GV->getInitializer()->getType()->isSingleValueType()) {
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL = GV->getParent()->getDataLayout();
|
2015-12-23 03:16:50 +08:00
|
|
|
if (SRAGlobal(GV, DL))
|
2015-03-05 02:43:29 +08:00
|
|
|
return true;
|
2015-10-12 21:20:52 +08:00
|
|
|
} else if (GS.StoredType == GlobalStatus::StoredOnce && GS.StoredOnceValue) {
|
2011-01-18 12:36:06 +08:00
|
|
|
// If the initial value for the global was an undef value, and if only
|
|
|
|
// one other value was stored into it, we can just change the
|
|
|
|
// initializer to be the stored value, then delete all stores to the
|
|
|
|
// global. This allows us to mark it constant.
|
|
|
|
if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
|
|
|
|
if (isa<UndefValue>(GV->getInitializer())) {
|
|
|
|
// Change the initial value here.
|
|
|
|
GV->setInitializer(SOVConstant);
|
|
|
|
|
|
|
|
// Clean up any obviously simplifiable users now.
|
2014-02-21 08:06:31 +08:00
|
|
|
CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
|
2011-01-18 12:36:06 +08:00
|
|
|
|
|
|
|
if (GV->use_empty()) {
|
|
|
|
DEBUG(dbgs() << " *** Substituting initializer allowed us to "
|
2012-07-24 15:21:08 +08:00
|
|
|
<< "simplify all users and delete global!\n");
|
2011-01-18 12:36:06 +08:00
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
}
|
|
|
|
++NumSubstitute;
|
|
|
|
return true;
|
2004-10-09 04:59:28 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2011-01-18 12:36:06 +08:00
|
|
|
// Try to optimize globals based on the knowledge that only one value
|
|
|
|
// (besides its initializer) is ever stored to the global.
|
2015-12-23 03:16:50 +08:00
|
|
|
if (optimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, DL, TLI))
|
2004-10-09 04:59:28 +08:00
|
|
|
return true;
|
2004-10-17 02:09:00 +08:00
|
|
|
|
2014-03-23 12:22:31 +08:00
|
|
|
// Otherwise, if the global was not a boolean, we can shrink it to be a
|
|
|
|
// boolean.
|
2013-09-10 06:00:13 +08:00
|
|
|
if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) {
|
|
|
|
if (GS.Ordering == NotAtomic) {
|
2014-03-23 12:22:31 +08:00
|
|
|
if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
|
2013-09-10 06:00:13 +08:00
|
|
|
++NumShrunkToBool;
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-18 12:36:06 +08:00
|
|
|
}
|
2013-09-10 06:00:13 +08:00
|
|
|
}
|
2004-10-09 04:59:28 +08:00
|
|
|
}
|
2011-01-18 12:36:06 +08:00
|
|
|
|
2004-10-09 04:59:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Walk all of the direct calls of the specified function, changing them to
|
|
|
|
/// FastCC.
|
2005-05-09 06:18:06 +08:00
|
|
|
static void ChangeCalleesToFastCall(Function *F) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : F->users()) {
|
|
|
|
if (isa<BlockAddress>(U))
|
2012-05-12 16:30:16 +08:00
|
|
|
continue;
|
2014-03-09 11:16:01 +08:00
|
|
|
CallSite CS(cast<Instruction>(U));
|
|
|
|
CS.setCallingConv(CallingConv::Fast);
|
2005-05-09 06:18:06 +08:00
|
|
|
}
|
|
|
|
}
|
2004-10-09 04:59:28 +08:00
|
|
|
|
2012-12-08 07:16:57 +08:00
|
|
|
static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs) {
|
2008-03-13 01:45:29 +08:00
|
|
|
for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
|
2013-01-26 07:09:36 +08:00
|
|
|
unsigned Index = Attrs.getSlotIndex(i);
|
|
|
|
if (!Attrs.getSlotAttributes(i).hasAttribute(Index, Attribute::Nest))
|
2008-02-19 01:32:13 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// There can be only one.
|
2013-01-26 07:09:36 +08:00
|
|
|
return Attrs.removeAttribute(C, Index, Attribute::Nest);
|
2008-02-17 04:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RemoveNestAttribute(Function *F) {
|
2012-10-14 14:39:53 +08:00
|
|
|
F->setAttributes(StripNest(F->getContext(), F->getAttributes()));
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : F->users()) {
|
|
|
|
if (isa<BlockAddress>(U))
|
2012-05-12 16:30:16 +08:00
|
|
|
continue;
|
2014-03-09 11:16:01 +08:00
|
|
|
CallSite CS(cast<Instruction>(U));
|
|
|
|
CS.setAttributes(StripNest(F->getContext(), CS.getAttributes()));
|
2008-02-17 04:56:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:57:30 +08:00
|
|
|
/// Return true if this is a calling convention that we'd like to change. The
|
|
|
|
/// idea here is that we don't want to mess with the convention if the user
|
|
|
|
/// explicitly requested something with performance implications like coldcc,
|
|
|
|
/// GHC, or anyregcc.
|
|
|
|
static bool isProfitableToMakeFastCC(Function *F) {
|
|
|
|
CallingConv::ID CC = F->getCallingConv();
|
|
|
|
// FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
|
|
|
|
return CC == CallingConv::C || CC == CallingConv::X86_ThisCall;
|
|
|
|
}
|
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
bool GlobalOpt::OptimizeFunctions(Module &M) {
|
2004-10-07 12:16:33 +08:00
|
|
|
bool Changed = false;
|
2005-09-26 09:43:45 +08:00
|
|
|
// Optimize functions.
|
|
|
|
for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
|
2015-10-14 01:51:03 +08:00
|
|
|
Function *F = &*FI++;
|
2009-03-06 18:21:56 +08:00
|
|
|
// Functions without names cannot be referenced outside this module.
|
2014-07-01 23:26:50 +08:00
|
|
|
if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage())
|
2009-03-06 18:21:56 +08:00
|
|
|
F->setLinkage(GlobalValue::InternalLinkage);
|
2014-10-08 15:23:31 +08:00
|
|
|
|
2015-12-23 03:38:07 +08:00
|
|
|
if (deleteIfDead(*F)) {
|
2005-09-26 09:43:45 +08:00
|
|
|
Changed = true;
|
2015-12-23 03:26:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-12-23 04:43:30 +08:00
|
|
|
|
|
|
|
Changed |= processGlobal(*F);
|
|
|
|
|
2015-12-23 03:26:18 +08:00
|
|
|
if (!F->hasLocalLinkage())
|
|
|
|
continue;
|
|
|
|
if (isProfitableToMakeFastCC(F) && !F->isVarArg() &&
|
|
|
|
!F->hasAddressTaken()) {
|
|
|
|
// If this function has a calling convention worth changing, is not a
|
|
|
|
// varargs function, and is only called directly, promote it to use the
|
|
|
|
// Fast calling convention.
|
|
|
|
F->setCallingConv(CallingConv::Fast);
|
|
|
|
ChangeCalleesToFastCall(F);
|
|
|
|
++NumFastCallFns;
|
|
|
|
Changed = true;
|
|
|
|
}
|
2008-02-17 04:56:04 +08:00
|
|
|
|
2015-12-23 03:26:18 +08:00
|
|
|
if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) &&
|
|
|
|
!F->hasAddressTaken()) {
|
|
|
|
// The function is not used by a trampoline intrinsic, so it is safe
|
|
|
|
// to remove the 'nest' attribute.
|
|
|
|
RemoveNestAttribute(F);
|
|
|
|
++NumNestRemoved;
|
|
|
|
Changed = true;
|
2005-09-26 09:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2004-10-07 12:16:33 +08:00
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
bool GlobalOpt::OptimizeGlobalVars(Module &M) {
|
|
|
|
bool Changed = false;
|
2014-06-28 02:19:56 +08:00
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
|
|
|
|
GVI != E; ) {
|
2015-10-14 01:51:03 +08:00
|
|
|
GlobalVariable *GV = &*GVI++;
|
2009-03-06 18:21:56 +08:00
|
|
|
// Global variables without names cannot be referenced outside this module.
|
2014-07-01 23:26:50 +08:00
|
|
|
if (!GV->hasName() && !GV->isDeclaration() && !GV->hasLocalLinkage())
|
2009-03-06 18:21:56 +08:00
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
2009-11-24 00:22:21 +08:00
|
|
|
// Simplify the initializer.
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
|
2015-03-05 02:43:29 +08:00
|
|
|
auto &DL = M.getDataLayout();
|
2015-03-10 10:37:25 +08:00
|
|
|
Constant *New = ConstantFoldConstantExpression(CE, DL, TLI);
|
2009-11-24 00:22:21 +08:00
|
|
|
if (New && New != CE)
|
|
|
|
GV->setInitializer(New);
|
|
|
|
}
|
2011-01-20 00:32:21 +08:00
|
|
|
|
2015-12-23 04:43:30 +08:00
|
|
|
if (deleteIfDead(*GV)) {
|
|
|
|
Changed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed |= processGlobal(*GV);
|
2005-09-26 09:43:45 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2012-12-07 05:57:16 +08:00
|
|
|
static inline bool
|
2010-12-07 12:33:29 +08:00
|
|
|
isSimpleEnoughValueToCommit(Constant *C,
|
2015-03-05 02:43:29 +08:00
|
|
|
SmallPtrSetImpl<Constant *> &SimpleConstants,
|
|
|
|
const DataLayout &DL);
|
2010-12-07 12:33:29 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Return true if the specified constant can be handled by the code generator.
|
|
|
|
/// We don't want to generate something like:
|
2010-12-07 12:33:29 +08:00
|
|
|
/// void *X = &X/42;
|
|
|
|
/// because the code generator doesn't have a relocation that can handle that.
|
|
|
|
///
|
|
|
|
/// This function should be called if C was not found (but just got inserted)
|
|
|
|
/// in SimpleConstants to avoid having to rescan the same constants all the
|
|
|
|
/// time.
|
2015-03-05 02:43:29 +08:00
|
|
|
static bool
|
|
|
|
isSimpleEnoughValueToCommitHelper(Constant *C,
|
|
|
|
SmallPtrSetImpl<Constant *> &SimpleConstants,
|
|
|
|
const DataLayout &DL) {
|
2014-06-26 11:02:19 +08:00
|
|
|
// Simple global addresses are supported, do not allow dllimport or
|
|
|
|
// thread-local globals.
|
2014-06-24 14:53:45 +08:00
|
|
|
if (auto *GV = dyn_cast<GlobalValue>(C))
|
2014-06-26 11:02:19 +08:00
|
|
|
return !GV->hasDLLImportStorageClass() && !GV->isThreadLocal();
|
2014-06-24 14:53:45 +08:00
|
|
|
|
|
|
|
// Simple integer, undef, constant aggregate zero, etc are all supported.
|
|
|
|
if (C->getNumOperands() == 0 || isa<BlockAddress>(C))
|
2010-12-07 12:33:29 +08:00
|
|
|
return true;
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2010-12-07 12:33:29 +08:00
|
|
|
// Aggregate values are safe if all their elements are.
|
|
|
|
if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
|
|
|
|
isa<ConstantVector>(C)) {
|
2015-06-26 04:51:38 +08:00
|
|
|
for (Value *Op : C->operands())
|
|
|
|
if (!isSimpleEnoughValueToCommit(cast<Constant>(Op), SimpleConstants, DL))
|
2010-12-07 12:33:29 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2010-12-07 12:33:29 +08:00
|
|
|
// We don't know exactly what relocations are allowed in constant expressions,
|
|
|
|
// so we allow &global+constantoffset, which is safe and uniformly supported
|
|
|
|
// across targets.
|
|
|
|
ConstantExpr *CE = cast<ConstantExpr>(C);
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::BitCast:
|
2012-01-06 07:03:32 +08:00
|
|
|
// Bitcast is fine if the casted value is fine.
|
2014-02-21 08:06:31 +08:00
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
|
2012-01-06 07:03:32 +08:00
|
|
|
|
2010-12-07 12:33:29 +08:00
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::PtrToInt:
|
2012-01-06 07:03:32 +08:00
|
|
|
// int <=> ptr is fine if the int type is the same size as the
|
|
|
|
// pointer type.
|
2015-03-05 02:43:29 +08:00
|
|
|
if (DL.getTypeSizeInBits(CE->getType()) !=
|
|
|
|
DL.getTypeSizeInBits(CE->getOperand(0)->getType()))
|
2012-01-06 07:03:32 +08:00
|
|
|
return false;
|
2014-02-21 08:06:31 +08:00
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2010-12-07 12:33:29 +08:00
|
|
|
// GEP is fine if it is simple + constant offset.
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isa<ConstantInt>(CE->getOperand(i)))
|
|
|
|
return false;
|
2014-02-21 08:06:31 +08:00
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2010-12-07 12:33:29 +08:00
|
|
|
case Instruction::Add:
|
|
|
|
// We allow simple+cst.
|
|
|
|
if (!isa<ConstantInt>(CE->getOperand(1)))
|
|
|
|
return false;
|
2014-02-21 08:06:31 +08:00
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
|
2010-12-07 12:33:29 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-07 05:57:16 +08:00
|
|
|
static inline bool
|
2010-12-07 12:33:29 +08:00
|
|
|
isSimpleEnoughValueToCommit(Constant *C,
|
2015-03-05 02:43:29 +08:00
|
|
|
SmallPtrSetImpl<Constant *> &SimpleConstants,
|
|
|
|
const DataLayout &DL) {
|
2010-12-07 12:33:29 +08:00
|
|
|
// If we already checked this constant, we win.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!SimpleConstants.insert(C).second)
|
|
|
|
return true;
|
2010-12-07 12:33:29 +08:00
|
|
|
// Check the constant.
|
2014-02-21 08:06:31 +08:00
|
|
|
return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, DL);
|
2010-12-07 12:33:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Return true if this constant is simple enough for us to understand. In
|
|
|
|
/// particular, if it is a cast to anything other than from one pointer type to
|
|
|
|
/// another pointer type, we punt. We basically just support direct accesses to
|
|
|
|
/// globals and GEP's of globals. This should be kept up to date with
|
|
|
|
/// CommitValueTo.
|
2009-11-06 12:27:31 +08:00
|
|
|
static bool isSimpleEnoughPointerToCommit(Constant *C) {
|
2009-09-08 06:42:05 +08:00
|
|
|
// Conservatively, avoid aggregate types. This is because we don't
|
|
|
|
// want to worry about them partially overlapping other stores.
|
|
|
|
if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
|
|
|
|
return false;
|
|
|
|
|
2009-09-08 06:31:26 +08:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
2014-06-24 14:53:45 +08:00
|
|
|
// Do not allow weak/*_odr/linkonce linkage or external globals.
|
2010-10-20 00:47:23 +08:00
|
|
|
return GV->hasUniqueInitializer();
|
2009-09-08 06:31:26 +08:00
|
|
|
|
2011-01-15 06:31:13 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
2005-09-26 14:52:44 +08:00
|
|
|
// Handle a constantexpr gep.
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr &&
|
2009-09-08 06:40:13 +08:00
|
|
|
isa<GlobalVariable>(CE->getOperand(0)) &&
|
|
|
|
cast<GEPOperator>(CE)->isInBounds()) {
|
2005-09-26 14:52:44 +08:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
|
2010-10-20 00:47:23 +08:00
|
|
|
// Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
|
2009-09-08 06:31:26 +08:00
|
|
|
// external globals.
|
2010-10-20 00:47:23 +08:00
|
|
|
if (!GV->hasUniqueInitializer())
|
2009-09-08 06:31:26 +08:00
|
|
|
return false;
|
2009-09-08 06:44:55 +08:00
|
|
|
|
|
|
|
// The first index must be zero.
|
2014-03-02 20:27:27 +08:00
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(*std::next(CE->op_begin()));
|
2009-09-08 06:44:55 +08:00
|
|
|
if (!CI || !CI->isZero()) return false;
|
|
|
|
|
|
|
|
// The remaining indices must be compile-time known integers within the
|
2009-09-11 07:37:55 +08:00
|
|
|
// notional bounds of the corresponding static array types.
|
|
|
|
if (!CE->isGEPWithNoNotionalOverIndexing())
|
|
|
|
return false;
|
2009-09-08 06:44:55 +08:00
|
|
|
|
2009-10-06 00:36:26 +08:00
|
|
|
return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2011-01-15 06:19:20 +08:00
|
|
|
// A constantexpr bitcast from a pointer to another pointer is a no-op,
|
|
|
|
// and we know how to evaluate it by moving the bitcast from the pointer
|
|
|
|
// operand to the value operand.
|
|
|
|
} else if (CE->getOpcode() == Instruction::BitCast &&
|
2011-01-16 10:05:10 +08:00
|
|
|
isa<GlobalVariable>(CE->getOperand(0))) {
|
2011-01-15 06:19:20 +08:00
|
|
|
// Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
|
|
|
|
// external globals.
|
2011-01-16 10:05:10 +08:00
|
|
|
return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer();
|
2005-09-26 14:52:44 +08:00
|
|
|
}
|
2011-01-15 06:31:13 +08:00
|
|
|
}
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2005-09-26 12:44:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Evaluate a piece of a constantexpr store into a global initializer. This
|
|
|
|
/// returns 'Init' modified to reflect 'Val' stored into it. At this point, the
|
|
|
|
/// GEP operands of Addr [0, OpNo) have been stepped into.
|
2015-07-23 06:26:54 +08:00
|
|
|
static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
|
|
|
|
ConstantExpr *Addr, unsigned OpNo) {
|
|
|
|
// Base case of the recursion.
|
|
|
|
if (OpNo == Addr->getNumOperands()) {
|
|
|
|
assert(Val->getType() == Init->getType() && "Type mismatch!");
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<Constant*, 32> Elts;
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(Init->getType())) {
|
|
|
|
// Break up the constant into its elements.
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
|
|
|
|
Elts.push_back(Init->getAggregateElement(i));
|
|
|
|
|
|
|
|
// Replace the element that we are supposed to.
|
|
|
|
ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
|
|
|
|
unsigned Idx = CU->getZExtValue();
|
|
|
|
assert(Idx < STy->getNumElements() && "Struct index out of range!");
|
|
|
|
Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
|
|
|
|
|
|
|
|
// Return the modified struct.
|
|
|
|
return ConstantStruct::get(STy, Elts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
|
|
|
|
SequentialType *InitTy = cast<SequentialType>(Init->getType());
|
|
|
|
|
|
|
|
uint64_t NumElts;
|
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(InitTy))
|
|
|
|
NumElts = ATy->getNumElements();
|
|
|
|
else
|
|
|
|
NumElts = InitTy->getVectorNumElements();
|
|
|
|
|
|
|
|
// Break up the array into elements.
|
|
|
|
for (uint64_t i = 0, e = NumElts; i != e; ++i)
|
|
|
|
Elts.push_back(Init->getAggregateElement(i));
|
|
|
|
|
|
|
|
assert(CI->getZExtValue() < NumElts);
|
|
|
|
Elts[CI->getZExtValue()] =
|
|
|
|
EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
|
|
|
|
|
|
|
|
if (Init->getType()->isArrayTy())
|
|
|
|
return ConstantArray::get(cast<ArrayType>(InitTy), Elts);
|
|
|
|
return ConstantVector::get(Elts);
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// We have decided that Addr (which satisfies the predicate
|
2015-07-23 06:26:54 +08:00
|
|
|
/// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen.
|
|
|
|
static void CommitValueTo(Constant *Val, Constant *Addr) {
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
|
|
|
|
assert(GV->hasInitializer());
|
|
|
|
GV->setInitializer(Val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantExpr *CE = cast<ConstantExpr>(Addr);
|
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
|
|
|
|
GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));
|
|
|
|
}
|
|
|
|
|
2012-02-20 11:25:59 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// This class evaluates LLVM IR, producing the Constant representing each SSA
|
|
|
|
/// instruction. Changes to global variables are stored in a mapping that can
|
|
|
|
/// be iterated over after the evaluation is complete. Once an evaluation call
|
|
|
|
/// fails, the evaluation object should not be reused.
|
2012-02-20 11:25:59 +08:00
|
|
|
class Evaluator {
|
2012-02-20 07:26:27 +08:00
|
|
|
public:
|
2015-03-05 02:43:29 +08:00
|
|
|
Evaluator(const DataLayout &DL, const TargetLibraryInfo *TLI)
|
|
|
|
: DL(DL), TLI(TLI) {
|
2014-05-03 23:50:37 +08:00
|
|
|
ValueStack.emplace_back();
|
2012-02-20 07:26:27 +08:00
|
|
|
}
|
|
|
|
|
2012-02-20 11:25:59 +08:00
|
|
|
~Evaluator() {
|
2014-04-22 04:49:36 +08:00
|
|
|
for (auto &Tmp : AllocaTmps)
|
2012-02-20 07:26:27 +08:00
|
|
|
// If there are still users of the alloca, the program is doing something
|
|
|
|
// silly, e.g. storing the address of the alloca somewhere and using it
|
|
|
|
// later. Since this is undefined, we'll just make it be null.
|
|
|
|
if (!Tmp->use_empty())
|
|
|
|
Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Evaluate a call to function F, returning true if successful, false if we
|
|
|
|
/// can't evaluate it. ActualArgs contains the formal arguments for the
|
|
|
|
/// function.
|
2012-02-20 07:26:27 +08:00
|
|
|
bool EvaluateFunction(Function *F, Constant *&RetVal,
|
|
|
|
const SmallVectorImpl<Constant*> &ActualArgs);
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Evaluate all instructions in block BB, returning true if successful, false
|
|
|
|
/// if we can't evaluate it. NewBB returns the next BB that control flows
|
|
|
|
/// into, or null upon return.
|
2012-02-20 07:26:27 +08:00
|
|
|
bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
|
|
|
|
|
|
|
|
Constant *getVal(Value *V) {
|
|
|
|
if (Constant *CV = dyn_cast<Constant>(V)) return CV;
|
2014-05-03 23:50:37 +08:00
|
|
|
Constant *R = ValueStack.back().lookup(V);
|
2012-02-20 07:26:27 +08:00
|
|
|
assert(R && "Reference to an uncomputed value!");
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setVal(Value *V, Constant *C) {
|
2014-05-03 23:50:37 +08:00
|
|
|
ValueStack.back()[V] = C;
|
2012-02-20 07:26:27 +08:00
|
|
|
}
|
|
|
|
|
2015-07-23 06:26:54 +08:00
|
|
|
const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
|
|
|
|
return MutatedMemory;
|
2012-02-20 07:26:27 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 13:55:13 +08:00
|
|
|
const SmallPtrSetImpl<GlobalVariable*> &getInvariants() const {
|
2012-02-20 07:26:27 +08:00
|
|
|
return Invariants;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Constant *ComputeLoadResult(Constant *P);
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// As we compute SSA register values, we store their contents here. The back
|
|
|
|
/// of the deque contains the current function and the stack contains the
|
|
|
|
/// values in the calling frames.
|
2014-05-03 23:50:37 +08:00
|
|
|
std::deque<DenseMap<Value*, Constant*>> ValueStack;
|
2012-02-20 07:26:27 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// This is used to detect recursion. In pathological situations we could hit
|
|
|
|
/// exponential behavior, but at least there is nothing unbounded.
|
2012-02-20 07:26:27 +08:00
|
|
|
SmallVector<Function*, 4> CallStack;
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// For each store we execute, we update this map. Loads check this to get
|
|
|
|
/// the most up-to-date value. If evaluation is successful, this state is
|
|
|
|
/// committed to the process.
|
2015-07-23 06:26:54 +08:00
|
|
|
DenseMap<Constant*, Constant*> MutatedMemory;
|
2012-02-20 07:26:27 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// To 'execute' an alloca, we create a temporary global variable to represent
|
|
|
|
/// its body. This vector is needed so we can delete the temporary globals
|
|
|
|
/// when we are done.
|
2014-04-22 04:49:36 +08:00
|
|
|
SmallVector<std::unique_ptr<GlobalVariable>, 32> AllocaTmps;
|
2012-02-20 07:26:27 +08:00
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// These global variables have been marked invariant by the static
|
|
|
|
/// constructor.
|
2012-02-20 07:26:27 +08:00
|
|
|
SmallPtrSet<GlobalVariable*, 8> Invariants;
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// These are constants we have checked and know to be simple enough to live
|
|
|
|
/// in a static initializer of a global.
|
2012-02-20 07:26:27 +08:00
|
|
|
SmallPtrSet<Constant*, 8> SimpleConstants;
|
|
|
|
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL;
|
2012-02-20 07:26:27 +08:00
|
|
|
const TargetLibraryInfo *TLI;
|
|
|
|
};
|
|
|
|
|
2012-02-20 11:25:59 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Return the value that would be computed by a load from P after the stores
|
|
|
|
/// reflected by 'memory' have been performed. If we can't decide, return null.
|
2012-02-20 11:25:59 +08:00
|
|
|
Constant *Evaluator::ComputeLoadResult(Constant *P) {
|
2005-09-26 13:15:37 +08:00
|
|
|
// If this memory location has been recently stored, use the stored value: it
|
|
|
|
// is the most up-to-date.
|
2015-07-23 06:26:54 +08:00
|
|
|
DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P);
|
|
|
|
if (I != MutatedMemory.end()) return I->second;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2005-09-26 13:15:37 +08:00
|
|
|
// Access it.
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
|
2009-08-20 02:20:44 +08:00
|
|
|
if (GV->hasDefinitiveInitializer())
|
2005-09-26 13:15:37 +08:00
|
|
|
return GV->getInitializer();
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2005-09-26 13:15:37 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2005-09-26 14:52:44 +08:00
|
|
|
// Handle a constantexpr getelementptr.
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr &&
|
|
|
|
isa<GlobalVariable>(CE->getOperand(0))) {
|
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
|
2009-08-20 02:20:44 +08:00
|
|
|
if (GV->hasDefinitiveInitializer())
|
2009-10-06 00:36:26 +08:00
|
|
|
return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
|
2005-09-26 14:52:44 +08:00
|
|
|
}
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr; // don't know how to evaluate.
|
2005-09-26 13:15:37 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Evaluate all instructions in block BB, returning true if successful, false
|
|
|
|
/// if we can't evaluate it. NewBB returns the next BB that control flows into,
|
|
|
|
/// or null upon return.
|
2012-02-20 11:25:59 +08:00
|
|
|
bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
|
|
|
|
BasicBlock *&NextBB) {
|
2005-09-26 12:44:35 +08:00
|
|
|
// This is the main evaluation loop.
|
|
|
|
while (1) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Constant *InstResult = nullptr;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
|
|
|
|
|
2005-09-26 12:44:35 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
|
2013-01-12 04:07:53 +08:00
|
|
|
if (!SI->isSimple()) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Store is not simple! Can not evaluate.\n");
|
|
|
|
return false; // no volatile/atomic accesses.
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2012-02-20 07:26:27 +08:00
|
|
|
Constant *Ptr = getVal(SI->getOperand(1));
|
2013-01-12 04:07:53 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
|
2015-03-10 10:37:25 +08:00
|
|
|
Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "; To: " << *Ptr << "\n");
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
|
|
|
if (!isSimpleEnoughPointerToCommit(Ptr)) {
|
2005-09-26 12:44:35 +08:00
|
|
|
// If this is too complex for us to commit, reject it.
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Pointer is too complex for us to evaluate store.");
|
2005-09-27 12:45:34 +08:00
|
|
|
return false;
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2012-02-20 07:26:27 +08:00
|
|
|
Constant *Val = getVal(SI->getOperand(0));
|
2010-12-07 12:33:29 +08:00
|
|
|
|
|
|
|
// If this might be too difficult for the backend to handle (e.g. the addr
|
|
|
|
// of one global variable divided by another) then we can't commit it.
|
2014-02-21 08:06:31 +08:00
|
|
|
if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, DL)) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Store value is too complex to evaluate store. " << *Val
|
|
|
|
<< "\n");
|
2010-12-07 12:33:29 +08:00
|
|
|
return false;
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2013-01-12 04:07:53 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2011-01-15 06:19:20 +08:00
|
|
|
if (CE->getOpcode() == Instruction::BitCast) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Attempting to resolve bitcast on constant ptr.\n");
|
2011-01-15 06:19:20 +08:00
|
|
|
// If we're evaluating a store through a bitcast, then we need
|
|
|
|
// to pull the bitcast off the pointer type and push it onto the
|
|
|
|
// stored value.
|
2011-01-16 10:05:10 +08:00
|
|
|
Ptr = CE->getOperand(0);
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2012-02-06 16:24:44 +08:00
|
|
|
Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType();
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2011-01-16 12:33:33 +08:00
|
|
|
// In order to push the bitcast onto the stored value, a bitcast
|
|
|
|
// from NewTy to Val's type must be legal. If it's not, we can try
|
|
|
|
// introspecting NewTy to find a legal conversion.
|
|
|
|
while (!Val->getType()->canLosslesslyBitCastTo(NewTy)) {
|
|
|
|
// If NewTy is a struct, we can convert the pointer to the struct
|
|
|
|
// into a pointer to its first member.
|
|
|
|
// FIXME: This could be extended to support arrays as well.
|
2011-07-18 12:54:35 +08:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(NewTy)) {
|
2011-01-16 12:33:33 +08:00
|
|
|
NewTy = STy->getTypeAtIndex(0U);
|
|
|
|
|
2012-02-06 16:24:44 +08:00
|
|
|
IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32);
|
2011-01-16 12:33:33 +08:00
|
|
|
Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
|
|
|
|
Constant * const IdxList[] = {IdxZero, IdxZero};
|
|
|
|
|
2015-04-03 02:55:32 +08:00
|
|
|
Ptr = ConstantExpr::getGetElementPtr(nullptr, Ptr, IdxList);
|
2012-02-22 06:08:06 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
|
2015-03-10 10:37:25 +08:00
|
|
|
Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
|
2012-02-22 06:08:06 +08:00
|
|
|
|
2011-01-16 12:33:33 +08:00
|
|
|
// If we can't improve the situation by introspecting NewTy,
|
|
|
|
// we have to give up.
|
|
|
|
} else {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
|
|
|
|
"evaluate.\n");
|
2012-02-06 16:24:44 +08:00
|
|
|
return false;
|
2011-01-16 12:33:33 +08:00
|
|
|
}
|
2011-01-15 06:19:20 +08:00
|
|
|
}
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2011-01-16 12:33:33 +08:00
|
|
|
// If we found compatible types, go ahead and push the bitcast
|
|
|
|
// onto the stored value.
|
2011-01-15 06:19:20 +08:00
|
|
|
Val = ConstantExpr::getBitCast(Val, NewTy);
|
2013-01-12 04:07:53 +08:00
|
|
|
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Evaluated bitcast: " << *Val << "\n");
|
2011-01-15 06:19:20 +08:00
|
|
|
}
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2015-07-23 06:26:54 +08:00
|
|
|
MutatedMemory[Ptr] = Val;
|
2005-09-26 12:44:35 +08:00
|
|
|
} else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
|
2009-07-30 02:55:55 +08:00
|
|
|
InstResult = ConstantExpr::get(BO->getOpcode(),
|
2012-02-20 07:26:27 +08:00
|
|
|
getVal(BO->getOperand(0)),
|
|
|
|
getVal(BO->getOperand(1)));
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: " << *InstResult
|
2013-01-12 07:08:52 +08:00
|
|
|
<< "\n");
|
2006-12-23 14:05:41 +08:00
|
|
|
} else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
|
2009-07-30 02:55:55 +08:00
|
|
|
InstResult = ConstantExpr::getCompare(CI->getPredicate(),
|
2012-02-20 07:26:27 +08:00
|
|
|
getVal(CI->getOperand(0)),
|
|
|
|
getVal(CI->getOperand(1)));
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult
|
2013-01-12 07:08:52 +08:00
|
|
|
<< "\n");
|
2005-09-26 12:44:35 +08:00
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
|
2009-07-30 02:55:55 +08:00
|
|
|
InstResult = ConstantExpr::getCast(CI->getOpcode(),
|
2012-02-20 07:26:27 +08:00
|
|
|
getVal(CI->getOperand(0)),
|
2005-09-26 12:44:35 +08:00
|
|
|
CI->getType());
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult
|
2013-01-12 07:08:52 +08:00
|
|
|
<< "\n");
|
2005-09-26 12:44:35 +08:00
|
|
|
} else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
|
2012-02-20 07:26:27 +08:00
|
|
|
InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
|
|
|
|
getVal(SI->getOperand(1)),
|
|
|
|
getVal(SI->getOperand(2)));
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
|
2013-01-12 07:08:52 +08:00
|
|
|
<< "\n");
|
2014-08-08 13:50:43 +08:00
|
|
|
} else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
|
|
|
|
InstResult = ConstantExpr::getExtractValue(
|
|
|
|
getVal(EVI->getAggregateOperand()), EVI->getIndices());
|
|
|
|
DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: " << *InstResult
|
|
|
|
<< "\n");
|
|
|
|
} else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {
|
|
|
|
InstResult = ConstantExpr::getInsertValue(
|
|
|
|
getVal(IVI->getAggregateOperand()),
|
|
|
|
getVal(IVI->getInsertedValueOperand()), IVI->getIndices());
|
|
|
|
DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: " << *InstResult
|
|
|
|
<< "\n");
|
2005-09-26 13:15:37 +08:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
|
2012-02-20 07:26:27 +08:00
|
|
|
Constant *P = getVal(GEP->getOperand(0));
|
2007-01-31 12:40:53 +08:00
|
|
|
SmallVector<Constant*, 8> GEPOps;
|
2008-05-29 09:59:18 +08:00
|
|
|
for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
|
|
|
|
i != e; ++i)
|
2012-02-20 07:26:27 +08:00
|
|
|
GEPOps.push_back(getVal(*i));
|
2011-07-21 23:15:37 +08:00
|
|
|
InstResult =
|
2015-04-03 02:55:32 +08:00
|
|
|
ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps,
|
|
|
|
cast<GEPOperator>(GEP)->isInBounds());
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult
|
2013-01-12 07:08:52 +08:00
|
|
|
<< "\n");
|
2005-09-26 13:15:37 +08:00
|
|
|
} else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
|
2013-01-12 04:07:53 +08:00
|
|
|
|
|
|
|
if (!LI->isSimple()) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Found a Load! Not a simple load, can not evaluate.\n");
|
|
|
|
return false; // no volatile/atomic accesses.
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
|
|
|
|
2012-02-22 06:08:06 +08:00
|
|
|
Constant *Ptr = getVal(LI->getOperand(0));
|
2013-01-12 04:07:53 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Found a constant pointer expression, constant "
|
|
|
|
"folding: " << *Ptr << "\n");
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2012-02-22 06:08:06 +08:00
|
|
|
InstResult = ComputeLoadResult(Ptr);
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!InstResult) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Failed to compute load result. Can not evaluate load."
|
|
|
|
"\n");
|
|
|
|
return false; // Could not evaluate load.
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Evaluated load: " << *InstResult << "\n");
|
2005-09-27 01:07:09 +08:00
|
|
|
} else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
|
2013-01-12 04:07:53 +08:00
|
|
|
if (AI->isArrayAllocation()) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
|
|
|
|
return false; // Cannot handle array allocs.
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2016-01-18 08:10:01 +08:00
|
|
|
Type *Ty = AI->getAllocatedType();
|
2014-04-22 04:49:36 +08:00
|
|
|
AllocaTmps.push_back(
|
|
|
|
make_unique<GlobalVariable>(Ty, false, GlobalValue::InternalLinkage,
|
|
|
|
UndefValue::get(Ty), AI->getName()));
|
|
|
|
InstResult = AllocaTmps.back().get();
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
|
2012-02-12 13:09:35 +08:00
|
|
|
} else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
|
2015-10-14 01:51:03 +08:00
|
|
|
CallSite CS(&*CurInst);
|
2009-03-10 07:04:12 +08:00
|
|
|
|
|
|
|
// Debug info can safely be ignored here.
|
2012-02-12 13:09:35 +08:00
|
|
|
if (isa<DbgInfoIntrinsic>(CS.getInstruction())) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Ignoring debug info.\n");
|
2009-03-10 07:04:12 +08:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-08 05:37:01 +08:00
|
|
|
// Cannot handle inline asm.
|
2013-01-12 04:07:53 +08:00
|
|
|
if (isa<InlineAsm>(CS.getCalledValue())) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Found inline asm, can not evaluate.\n");
|
|
|
|
return false;
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2006-07-08 05:37:01 +08:00
|
|
|
|
2012-02-17 14:59:21 +08:00
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
|
|
|
|
if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
|
2013-01-12 04:07:53 +08:00
|
|
|
if (MSI->isVolatile()) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Can not optimize a volatile memset " <<
|
|
|
|
"intrinsic.\n");
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-20 07:26:27 +08:00
|
|
|
Constant *Ptr = getVal(MSI->getDest());
|
|
|
|
Constant *Val = getVal(MSI->getValue());
|
|
|
|
Constant *DestVal = ComputeLoadResult(getVal(Ptr));
|
2012-02-17 14:59:21 +08:00
|
|
|
if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
|
|
|
|
// This memset is a no-op.
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Ignoring no-op memset.\n");
|
2012-02-17 14:59:21 +08:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
|
|
|
|
II->getIntrinsicID() == Intrinsic::lifetime_end) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Ignoring lifetime intrinsic.\n");
|
2012-02-17 14:59:21 +08:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::invariant_start) {
|
|
|
|
// We don't insert an entry into Values, as it doesn't have a
|
|
|
|
// meaningful return value.
|
2013-01-12 04:07:53 +08:00
|
|
|
if (!II->use_empty()) {
|
2013-12-05 13:44:44 +08:00
|
|
|
DEBUG(dbgs() << "Found unused invariant_start. Can't evaluate.\n");
|
2012-02-17 14:59:21 +08:00
|
|
|
return false;
|
2013-01-12 07:08:52 +08:00
|
|
|
}
|
2012-02-17 14:59:21 +08:00
|
|
|
ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
|
2012-02-21 07:32:26 +08:00
|
|
|
Value *PtrArg = getVal(II->getArgOperand(1));
|
|
|
|
Value *Ptr = PtrArg->stripPointerCasts();
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
|
2016-01-17 04:30:46 +08:00
|
|
|
Type *ElemTy = GV->getValueType();
|
2015-03-05 02:43:29 +08:00
|
|
|
if (!Size->isAllOnesValue() &&
|
2012-02-21 07:32:26 +08:00
|
|
|
Size->getValue().getLimitedValue() >=
|
2015-03-05 02:43:29 +08:00
|
|
|
DL.getTypeStoreSize(ElemTy)) {
|
2012-02-17 14:59:21 +08:00
|
|
|
Invariants.insert(GV);
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Found a global var that is an invariant: " << *GV
|
|
|
|
<< "\n");
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "Found a global var, but can not treat it as an "
|
|
|
|
"invariant.\n");
|
|
|
|
}
|
2012-02-17 14:59:21 +08:00
|
|
|
}
|
|
|
|
// Continue even if we do nothing.
|
2011-05-30 02:41:56 +08:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
2015-08-25 09:34:15 +08:00
|
|
|
} else if (II->getIntrinsicID() == Intrinsic::assume) {
|
|
|
|
DEBUG(dbgs() << "Skipping assume intrinsic.\n");
|
|
|
|
++CurInst;
|
|
|
|
continue;
|
2011-05-30 02:41:56 +08:00
|
|
|
}
|
2013-01-12 04:07:53 +08:00
|
|
|
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Unknown intrinsic. Can not evaluate.\n");
|
2011-05-30 02:41:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-09-27 12:45:34 +08:00
|
|
|
// Resolve function pointers.
|
2012-02-20 07:26:27 +08:00
|
|
|
Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue()));
|
2013-01-12 04:07:53 +08:00
|
|
|
if (!Callee || Callee->mayBeOverridden()) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Can not resolve function pointer.\n");
|
2012-02-12 13:09:35 +08:00
|
|
|
return false; // Cannot resolve.
|
2013-01-12 04:07:53 +08:00
|
|
|
}
|
2005-09-27 13:02:43 +08:00
|
|
|
|
2009-08-17 22:33:27 +08:00
|
|
|
SmallVector<Constant*, 8> Formals;
|
2012-02-12 13:09:35 +08:00
|
|
|
for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i)
|
2012-02-20 07:26:27 +08:00
|
|
|
Formals.push_back(getVal(*i));
|
2009-08-17 22:33:27 +08:00
|
|
|
|
2007-01-31 04:08:39 +08:00
|
|
|
if (Callee->isDeclaration()) {
|
2005-09-27 13:02:43 +08:00
|
|
|
// If this is a function we can constant fold, do it.
|
2011-12-02 05:29:16 +08:00
|
|
|
if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
|
2005-09-27 13:02:43 +08:00
|
|
|
InstResult = C;
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Constant folded function call. Result: " <<
|
|
|
|
*InstResult << "\n");
|
2005-09-27 13:02:43 +08:00
|
|
|
} else {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Can not constant fold function call.\n");
|
2005-09-27 13:02:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-12 04:07:53 +08:00
|
|
|
if (Callee->getFunctionType()->isVarArg()) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Can not constant fold vararg function call.\n");
|
2005-09-27 13:02:43 +08:00
|
|
|
return false;
|
2013-01-12 07:08:52 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
Constant *RetVal = nullptr;
|
2005-09-27 13:02:43 +08:00
|
|
|
// Execute the call, if successful, use the return value.
|
2014-05-03 23:50:37 +08:00
|
|
|
ValueStack.emplace_back();
|
2013-01-12 04:07:53 +08:00
|
|
|
if (!EvaluateFunction(Callee, RetVal, Formals)) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Failed to evaluate function.\n");
|
2005-09-27 13:02:43 +08:00
|
|
|
return false;
|
2013-01-12 07:08:52 +08:00
|
|
|
}
|
2014-04-22 04:49:36 +08:00
|
|
|
ValueStack.pop_back();
|
2005-09-27 13:02:43 +08:00
|
|
|
InstResult = RetVal;
|
2013-01-12 04:07:53 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
if (InstResult) {
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Successfully evaluated function. Result: " <<
|
|
|
|
InstResult << "\n\n");
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "Successfully evaluated function. Result: 0\n\n");
|
|
|
|
}
|
2005-09-27 13:02:43 +08:00
|
|
|
}
|
2006-11-03 04:25:50 +08:00
|
|
|
} else if (isa<TerminatorInst>(CurInst)) {
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found a terminator instruction.\n");
|
|
|
|
|
2005-09-26 12:57:38 +08:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
|
|
|
|
if (BI->isUnconditional()) {
|
2012-02-06 16:24:44 +08:00
|
|
|
NextBB = BI->getSuccessor(0);
|
2005-09-26 12:57:38 +08:00
|
|
|
} else {
|
2007-01-11 20:24:14 +08:00
|
|
|
ConstantInt *Cond =
|
2012-02-20 07:26:27 +08:00
|
|
|
dyn_cast<ConstantInt>(getVal(BI->getCondition()));
|
2007-01-13 02:30:11 +08:00
|
|
|
if (!Cond) return false; // Cannot determine.
|
2007-01-11 20:24:14 +08:00
|
|
|
|
2012-02-06 16:24:44 +08:00
|
|
|
NextBB = BI->getSuccessor(!Cond->getZExtValue());
|
2005-09-26 12:57:38 +08:00
|
|
|
}
|
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
|
|
|
|
ConstantInt *Val =
|
2012-02-20 07:26:27 +08:00
|
|
|
dyn_cast<ConstantInt>(getVal(SI->getCondition()));
|
2005-09-27 12:45:34 +08:00
|
|
|
if (!Val) return false; // Cannot determine.
|
2012-03-08 15:06:20 +08:00
|
|
|
NextBB = SI->findCaseValue(Val).getCaseSuccessor();
|
2009-10-29 13:51:50 +08:00
|
|
|
} else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
|
2012-02-20 07:26:27 +08:00
|
|
|
Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
|
2009-10-29 13:51:50 +08:00
|
|
|
if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
|
2012-02-06 16:24:44 +08:00
|
|
|
NextBB = BA->getBasicBlock();
|
2009-11-01 09:27:45 +08:00
|
|
|
else
|
|
|
|
return false; // Cannot determine.
|
2012-02-06 16:24:44 +08:00
|
|
|
} else if (isa<ReturnInst>(CurInst)) {
|
2014-04-25 13:29:35 +08:00
|
|
|
NextBB = nullptr;
|
2005-09-26 12:57:38 +08:00
|
|
|
} else {
|
2011-07-31 14:30:59 +08:00
|
|
|
// invoke, unwind, resume, unreachable.
|
2013-01-12 07:08:52 +08:00
|
|
|
DEBUG(dbgs() << "Can not handle terminator.");
|
2005-09-27 12:45:34 +08:00
|
|
|
return false; // Cannot handle this terminator.
|
2005-09-26 12:57:38 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2012-02-06 16:24:44 +08:00
|
|
|
// We succeeded at evaluating this block!
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Successfully evaluated block.\n");
|
2012-02-06 16:24:44 +08:00
|
|
|
return true;
|
2005-09-26 12:44:35 +08:00
|
|
|
} else {
|
|
|
|
// Did not know how to evaluate this!
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Failed to evaluate block due to unhandled instruction."
|
2013-01-12 07:08:52 +08:00
|
|
|
"\n");
|
2005-09-27 12:45:34 +08:00
|
|
|
return false;
|
2005-09-26 12:44:35 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2010-12-07 12:33:29 +08:00
|
|
|
if (!CurInst->use_empty()) {
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult))
|
2015-03-10 10:37:25 +08:00
|
|
|
InstResult = ConstantFoldConstantExpression(CE, DL, TLI);
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2015-10-14 01:51:03 +08:00
|
|
|
setVal(&*CurInst, InstResult);
|
2010-12-07 12:33:29 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2012-03-14 02:01:37 +08:00
|
|
|
// If we just processed an invoke, we finished evaluating the block.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
|
|
|
|
NextBB = II->getNormalDest();
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Found an invoke instruction. Finished Block.\n\n");
|
2012-03-14 02:01:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-09-26 12:44:35 +08:00
|
|
|
// Advance program counter.
|
|
|
|
++CurInst;
|
|
|
|
}
|
2005-09-27 12:27:01 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Evaluate a call to function F, returning true if successful, false if we
|
|
|
|
/// can't evaluate it. ActualArgs contains the formal arguments for the
|
|
|
|
/// function.
|
2012-02-20 11:25:59 +08:00
|
|
|
bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
|
|
|
|
const SmallVectorImpl<Constant*> &ActualArgs) {
|
2012-02-06 16:24:44 +08:00
|
|
|
// Check to see if this function is already executing (recursion). If so,
|
|
|
|
// bail out. TODO: we might want to accept limited recursion.
|
|
|
|
if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CallStack.push_back(F);
|
|
|
|
|
|
|
|
// Initialize arguments to the incoming values specified.
|
|
|
|
unsigned ArgNo = 0;
|
|
|
|
for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
|
|
|
|
++AI, ++ArgNo)
|
2015-10-14 01:51:03 +08:00
|
|
|
setVal(&*AI, ActualArgs[ArgNo]);
|
2012-02-06 16:24:44 +08:00
|
|
|
|
|
|
|
// ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
|
|
|
|
// we can only evaluate any one basic block at most once. This set keeps
|
|
|
|
// track of what we have executed so we can detect recursive cases etc.
|
|
|
|
SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
|
|
|
|
|
|
|
|
// CurBB - The current basic block we're evaluating.
|
2015-10-14 01:51:03 +08:00
|
|
|
BasicBlock *CurBB = &F->front();
|
2012-02-06 16:24:44 +08:00
|
|
|
|
2012-02-12 08:47:24 +08:00
|
|
|
BasicBlock::iterator CurInst = CurBB->begin();
|
|
|
|
|
2012-02-06 16:24:44 +08:00
|
|
|
while (1) {
|
2014-04-25 13:29:35 +08:00
|
|
|
BasicBlock *NextBB = nullptr; // Initialized to avoid compiler warnings.
|
2013-01-12 04:07:53 +08:00
|
|
|
DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
|
|
|
|
|
2012-02-20 07:26:27 +08:00
|
|
|
if (!EvaluateBlock(CurInst, NextBB))
|
2012-02-06 16:24:44 +08:00
|
|
|
return false;
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!NextBB) {
|
2012-02-06 16:24:44 +08:00
|
|
|
// Successfully running until there's no next block means that we found
|
|
|
|
// the return. Fill it the return value and pop the call stack.
|
|
|
|
ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
|
|
|
|
if (RI->getNumOperands())
|
2012-02-20 07:26:27 +08:00
|
|
|
RetVal = getVal(RI->getOperand(0));
|
2012-02-06 16:24:44 +08:00
|
|
|
CallStack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we succeeded in evaluating this control flow. See if we have
|
|
|
|
// executed the new block before. If so, we have a looping function,
|
|
|
|
// which we cannot evaluate in reasonable time.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!ExecutedBlocks.insert(NextBB).second)
|
2012-02-06 16:24:44 +08:00
|
|
|
return false; // looped!
|
|
|
|
|
|
|
|
// Okay, we have never been in this block before. Check to see if there
|
|
|
|
// are any PHI nodes. If so, evaluate them with information about where
|
|
|
|
// we came from.
|
2014-04-25 13:29:35 +08:00
|
|
|
PHINode *PN = nullptr;
|
2012-02-12 08:47:24 +08:00
|
|
|
for (CurInst = NextBB->begin();
|
|
|
|
(PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
|
2012-02-20 07:26:27 +08:00
|
|
|
setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
|
2012-02-06 16:24:44 +08:00
|
|
|
|
|
|
|
// Advance to the next block.
|
|
|
|
CurBB = NextBB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Evaluate static constructors in the function, if we can. Return true if we
|
|
|
|
/// can, false otherwise.
|
2015-03-05 02:43:29 +08:00
|
|
|
static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL,
|
2011-12-02 05:29:16 +08:00
|
|
|
const TargetLibraryInfo *TLI) {
|
2005-09-27 12:27:01 +08:00
|
|
|
// Call the function.
|
2014-02-21 08:06:31 +08:00
|
|
|
Evaluator Eval(DL, TLI);
|
2005-09-27 12:45:34 +08:00
|
|
|
Constant *RetValDummy;
|
2012-02-20 07:26:27 +08:00
|
|
|
bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
|
|
|
|
SmallVector<Constant*, 0>());
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2005-09-27 12:27:01 +08:00
|
|
|
if (EvalSuccess) {
|
2014-05-03 02:35:25 +08:00
|
|
|
++NumCtorsEvaluated;
|
|
|
|
|
2005-09-27 01:07:09 +08:00
|
|
|
// We succeeded at evaluation: commit the result.
|
2010-01-05 09:28:05 +08:00
|
|
|
DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
|
2015-07-23 06:26:54 +08:00
|
|
|
<< F->getName() << "' to " << Eval.getMutatedMemory().size()
|
|
|
|
<< " stores.\n");
|
|
|
|
for (DenseMap<Constant*, Constant*>::const_iterator I =
|
|
|
|
Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end();
|
|
|
|
I != E; ++I)
|
|
|
|
CommitValueTo(I->second, I->first);
|
2014-08-25 07:23:06 +08:00
|
|
|
for (GlobalVariable *GV : Eval.getInvariants())
|
|
|
|
GV->setConstant(true);
|
2005-09-27 01:07:09 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2005-09-27 12:27:01 +08:00
|
|
|
return EvalSuccess;
|
2005-09-26 12:44:35 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 05:52:38 +08:00
|
|
|
static int compareNames(Constant *const *A, Constant *const *B) {
|
2015-09-29 03:02:11 +08:00
|
|
|
return (*A)->stripPointerCasts()->getName().compare(
|
|
|
|
(*B)->stripPointerCasts()->getName());
|
2014-03-08 05:52:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
static void setUsedInitializer(GlobalVariable &V,
|
2014-08-19 15:44:27 +08:00
|
|
|
const SmallPtrSet<GlobalValue *, 8> &Init) {
|
2013-07-21 07:33:15 +08:00
|
|
|
if (Init.empty()) {
|
|
|
|
V.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-03 03:53:49 +08:00
|
|
|
// Type of pointer to the array of pointers.
|
|
|
|
PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2014-01-03 03:53:49 +08:00
|
|
|
SmallVector<llvm::Constant *, 8> UsedArray;
|
2014-08-21 13:55:13 +08:00
|
|
|
for (GlobalValue *GV : Init) {
|
2014-01-03 03:53:49 +08:00
|
|
|
Constant *Cast
|
2014-08-21 13:55:13 +08:00
|
|
|
= ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy);
|
2013-06-12 01:48:06 +08:00
|
|
|
UsedArray.push_back(Cast);
|
|
|
|
}
|
|
|
|
// Sort to get deterministic order.
|
2014-03-08 05:52:38 +08:00
|
|
|
array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
|
2013-06-12 01:48:06 +08:00
|
|
|
ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());
|
|
|
|
|
|
|
|
Module *M = V.getParent();
|
|
|
|
V.removeFromParent();
|
|
|
|
GlobalVariable *NV =
|
|
|
|
new GlobalVariable(*M, ATy, false, llvm::GlobalValue::AppendingLinkage,
|
|
|
|
llvm::ConstantArray::get(ATy, UsedArray), "");
|
|
|
|
NV->takeName(&V);
|
|
|
|
NV->setSection("llvm.metadata");
|
|
|
|
delete &V;
|
|
|
|
}
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
namespace {
|
2015-11-13 19:05:07 +08:00
|
|
|
/// An easy to access representation of llvm.used and llvm.compiler.used.
|
2013-06-12 01:48:06 +08:00
|
|
|
class LLVMUsed {
|
|
|
|
SmallPtrSet<GlobalValue *, 8> Used;
|
|
|
|
SmallPtrSet<GlobalValue *, 8> CompilerUsed;
|
|
|
|
GlobalVariable *UsedV;
|
|
|
|
GlobalVariable *CompilerUsedV;
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
public:
|
2013-07-25 10:50:08 +08:00
|
|
|
LLVMUsed(Module &M) {
|
2013-07-25 11:23:25 +08:00
|
|
|
UsedV = collectUsedGlobalVariables(M, Used, false);
|
|
|
|
CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true);
|
2013-06-12 01:48:06 +08:00
|
|
|
}
|
|
|
|
typedef SmallPtrSet<GlobalValue *, 8>::iterator iterator;
|
2014-08-25 07:23:06 +08:00
|
|
|
typedef iterator_range<iterator> used_iterator_range;
|
2013-06-12 01:48:06 +08:00
|
|
|
iterator usedBegin() { return Used.begin(); }
|
|
|
|
iterator usedEnd() { return Used.end(); }
|
2014-08-25 07:23:06 +08:00
|
|
|
used_iterator_range used() {
|
|
|
|
return used_iterator_range(usedBegin(), usedEnd());
|
|
|
|
}
|
2013-06-12 01:48:06 +08:00
|
|
|
iterator compilerUsedBegin() { return CompilerUsed.begin(); }
|
|
|
|
iterator compilerUsedEnd() { return CompilerUsed.end(); }
|
2014-08-25 07:23:06 +08:00
|
|
|
used_iterator_range compilerUsed() {
|
|
|
|
return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
|
|
|
|
}
|
2013-06-12 01:48:06 +08:00
|
|
|
bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
|
|
|
|
bool compilerUsedCount(GlobalValue *GV) const {
|
|
|
|
return CompilerUsed.count(GV);
|
|
|
|
}
|
|
|
|
bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
|
|
|
|
bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
|
2014-11-19 15:49:26 +08:00
|
|
|
bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
|
|
|
|
bool compilerUsedInsert(GlobalValue *GV) {
|
|
|
|
return CompilerUsed.insert(GV).second;
|
|
|
|
}
|
2013-06-12 01:48:06 +08:00
|
|
|
|
|
|
|
void syncVariablesAndSets() {
|
|
|
|
if (UsedV)
|
|
|
|
setUsedInitializer(*UsedV, Used);
|
|
|
|
if (CompilerUsedV)
|
|
|
|
setUsedInitializer(*CompilerUsedV, CompilerUsed);
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
|
|
|
|
if (GA.use_empty()) // No use at all.
|
|
|
|
return false;
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&
|
|
|
|
"We should have removed the duplicated "
|
2013-07-20 02:44:51 +08:00
|
|
|
"element from llvm.compiler.used");
|
2013-06-12 01:48:06 +08:00
|
|
|
if (!GA.hasOneUse())
|
|
|
|
// Strictly more than one use. So at least one is not in llvm.used and
|
2013-07-20 02:44:51 +08:00
|
|
|
// llvm.compiler.used.
|
2013-06-12 01:48:06 +08:00
|
|
|
return true;
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2013-07-20 02:44:51 +08:00
|
|
|
// Exactly one use. Check if it is in llvm.used or llvm.compiler.used.
|
2013-06-12 01:48:06 +08:00
|
|
|
return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
|
2013-05-10 01:22:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
|
|
|
|
const LLVMUsed &U) {
|
|
|
|
unsigned N = 2;
|
|
|
|
assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
|
|
|
|
"We should have removed the duplicated "
|
2013-07-20 02:44:51 +08:00
|
|
|
"element from llvm.compiler.used");
|
2013-06-12 01:48:06 +08:00
|
|
|
if (U.usedCount(&V) || U.compilerUsedCount(&V))
|
|
|
|
++N;
|
|
|
|
return V.hasNUsesOrMore(N);
|
2013-05-10 01:22:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
|
|
|
|
if (!GA.hasLocalLinkage())
|
|
|
|
return true;
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
return U.usedCount(&GA) || U.compilerUsedCount(&GA);
|
|
|
|
}
|
2013-05-10 01:22:59 +08:00
|
|
|
|
2014-08-21 13:55:13 +08:00
|
|
|
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
|
|
|
|
bool &RenameTarget) {
|
2013-06-12 01:48:06 +08:00
|
|
|
RenameTarget = false;
|
|
|
|
bool Ret = false;
|
|
|
|
if (hasUseOtherThanLLVMUsed(GA, U))
|
2013-05-10 01:22:59 +08:00
|
|
|
Ret = true;
|
2013-06-12 01:48:06 +08:00
|
|
|
|
|
|
|
// If the alias is externally visible, we may still be able to simplify it.
|
|
|
|
if (!mayHaveOtherReferences(GA, U))
|
|
|
|
return Ret;
|
|
|
|
|
|
|
|
// If the aliasee has internal linkage, give it the name and linkage
|
|
|
|
// of the alias, and delete the alias. This turns:
|
|
|
|
// define internal ... @f(...)
|
|
|
|
// @a = alias ... @f
|
|
|
|
// into:
|
|
|
|
// define ... @a(...)
|
|
|
|
Constant *Aliasee = GA.getAliasee();
|
|
|
|
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
|
|
|
|
if (!Target->hasLocalLinkage())
|
|
|
|
return Ret;
|
|
|
|
|
|
|
|
// Do not perform the transform if multiple aliases potentially target the
|
|
|
|
// aliasee. This check also ensures that it is safe to replace the section
|
|
|
|
// and other attributes of the aliasee with those of the alias.
|
|
|
|
if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
|
|
|
|
return Ret;
|
|
|
|
|
|
|
|
RenameTarget = true;
|
|
|
|
return true;
|
2013-05-10 01:22:59 +08:00
|
|
|
}
|
|
|
|
|
2009-03-06 18:21:56 +08:00
|
|
|
bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
|
2008-09-10 03:04:59 +08:00
|
|
|
bool Changed = false;
|
2013-06-12 01:48:06 +08:00
|
|
|
LLVMUsed Used(M);
|
|
|
|
|
2014-08-25 07:23:06 +08:00
|
|
|
for (GlobalValue *GV : Used.used())
|
|
|
|
Used.compilerUsedErase(GV);
|
2008-09-10 03:04:59 +08:00
|
|
|
|
2009-01-08 04:01:06 +08:00
|
|
|
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
|
2009-02-15 17:56:08 +08:00
|
|
|
I != E;) {
|
2015-12-23 03:50:22 +08:00
|
|
|
GlobalAlias *J = &*I++;
|
|
|
|
|
2009-03-06 18:21:56 +08:00
|
|
|
// Aliases without names cannot be referenced outside this module.
|
2014-07-01 23:26:50 +08:00
|
|
|
if (!J->hasName() && !J->isDeclaration() && !J->hasLocalLinkage())
|
2009-03-06 18:21:56 +08:00
|
|
|
J->setLinkage(GlobalValue::InternalLinkage);
|
2015-12-23 03:50:22 +08:00
|
|
|
|
|
|
|
if (deleteIfDead(*J)) {
|
|
|
|
Changed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-15 17:56:08 +08:00
|
|
|
// If the aliasee may change at link time, nothing can be done - bail out.
|
|
|
|
if (J->mayBeOverridden())
|
2008-09-10 03:04:59 +08:00
|
|
|
continue;
|
|
|
|
|
2009-02-15 17:56:08 +08:00
|
|
|
Constant *Aliasee = J->getAliasee();
|
2014-07-01 08:30:56 +08:00
|
|
|
GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());
|
|
|
|
// We can't trivially replace the alias with the aliasee if the aliasee is
|
|
|
|
// non-trivial in some way.
|
|
|
|
// TODO: Try to handle non-zero GEPs of local aliasees.
|
|
|
|
if (!Target)
|
|
|
|
continue;
|
2009-02-19 01:55:38 +08:00
|
|
|
Target->removeDeadConstantUsers();
|
2009-02-15 17:56:08 +08:00
|
|
|
|
|
|
|
// Make all users of the alias use the aliasee instead.
|
2013-06-12 01:48:06 +08:00
|
|
|
bool RenameTarget;
|
|
|
|
if (!hasUsesToReplace(*J, Used, RenameTarget))
|
2013-05-10 01:22:59 +08:00
|
|
|
continue;
|
2009-02-15 17:56:08 +08:00
|
|
|
|
2014-05-17 03:35:39 +08:00
|
|
|
J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType()));
|
2013-06-12 01:48:06 +08:00
|
|
|
++NumAliasesResolved;
|
|
|
|
Changed = true;
|
2009-02-15 17:56:08 +08:00
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
if (RenameTarget) {
|
2009-12-08 18:10:20 +08:00
|
|
|
// Give the aliasee the name, linkage and other attributes of the alias.
|
2015-10-14 01:51:03 +08:00
|
|
|
Target->takeName(&*J);
|
2009-12-08 18:10:20 +08:00
|
|
|
Target->setLinkage(J->getLinkage());
|
2014-02-13 10:18:36 +08:00
|
|
|
Target->setVisibility(J->getVisibility());
|
|
|
|
Target->setDLLStorageClass(J->getDLLStorageClass());
|
2013-06-12 01:48:06 +08:00
|
|
|
|
2015-10-14 01:51:03 +08:00
|
|
|
if (Used.usedErase(&*J))
|
2013-06-12 01:48:06 +08:00
|
|
|
Used.usedInsert(Target);
|
|
|
|
|
2015-10-14 01:51:03 +08:00
|
|
|
if (Used.compilerUsedErase(&*J))
|
2013-06-12 01:48:06 +08:00
|
|
|
Used.compilerUsedInsert(Target);
|
2013-06-13 00:45:47 +08:00
|
|
|
} else if (mayHaveOtherReferences(*J, Used))
|
2013-06-12 01:48:06 +08:00
|
|
|
continue;
|
|
|
|
|
2009-02-15 17:56:08 +08:00
|
|
|
// Delete the alias.
|
|
|
|
M.getAliasList().erase(J);
|
|
|
|
++NumAliasesRemoved;
|
|
|
|
Changed = true;
|
2008-09-10 03:04:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 01:48:06 +08:00
|
|
|
Used.syncVariablesAndSets();
|
|
|
|
|
2008-09-10 03:04:59 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2005-09-26 09:43:45 +08:00
|
|
|
|
2012-02-12 10:15:20 +08:00
|
|
|
static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) {
|
|
|
|
if (!TLI->has(LibFunc::cxa_atexit))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2012-02-12 10:15:20 +08:00
|
|
|
|
|
|
|
Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit));
|
2012-12-07 05:57:16 +08:00
|
|
|
|
2011-03-21 01:59:11 +08:00
|
|
|
if (!Fn)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2012-02-12 10:15:20 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *FTy = Fn->getFunctionType();
|
2012-12-07 05:57:16 +08:00
|
|
|
|
|
|
|
// Checking that the function has the right return type, the right number of
|
2011-03-21 03:51:13 +08:00
|
|
|
// parameters and that they all have pointer types should be enough.
|
|
|
|
if (!FTy->getReturnType()->isIntegerTy() ||
|
|
|
|
FTy->getNumParams() != 3 ||
|
2011-03-21 01:59:11 +08:00
|
|
|
!FTy->getParamType(0)->isPointerTy() ||
|
|
|
|
!FTy->getParamType(1)->isPointerTy() ||
|
|
|
|
!FTy->getParamType(2)->isPointerTy())
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-03-21 01:59:11 +08:00
|
|
|
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:05:07 +08:00
|
|
|
/// Returns whether the given function is an empty C++ destructor and can
|
|
|
|
/// therefore be eliminated.
|
2011-03-21 01:59:11 +08:00
|
|
|
/// Note that we assume that other optimization passes have already simplified
|
|
|
|
/// the code so we only look for a function with a single basic block, where
|
2012-02-10 00:28:15 +08:00
|
|
|
/// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and
|
|
|
|
/// other side-effect free instructions.
|
2011-03-21 04:16:43 +08:00
|
|
|
static bool cxxDtorIsEmpty(const Function &Fn,
|
|
|
|
SmallPtrSet<const Function *, 8> &CalledFunctions) {
|
2011-03-21 03:51:13 +08:00
|
|
|
// FIXME: We could eliminate C++ destructors if they're readonly/readnone and
|
2011-03-21 10:26:01 +08:00
|
|
|
// nounwind, but that doesn't seem worth doing.
|
2011-03-21 03:51:13 +08:00
|
|
|
if (Fn.isDeclaration())
|
|
|
|
return false;
|
2011-03-21 01:59:11 +08:00
|
|
|
|
|
|
|
if (++Fn.begin() != Fn.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const BasicBlock &EntryBlock = Fn.getEntryBlock();
|
|
|
|
for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I)) {
|
2011-03-21 22:54:40 +08:00
|
|
|
// Ignore debug intrinsics.
|
|
|
|
if (isa<DbgInfoIntrinsic>(CI))
|
|
|
|
continue;
|
|
|
|
|
2011-03-21 01:59:11 +08:00
|
|
|
const Function *CalledFn = CI->getCalledFunction();
|
|
|
|
|
|
|
|
if (!CalledFn)
|
|
|
|
return false;
|
|
|
|
|
2011-03-22 11:21:01 +08:00
|
|
|
SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions);
|
|
|
|
|
2011-03-21 03:51:13 +08:00
|
|
|
// Don't treat recursive functions as empty.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!NewCalledFunctions.insert(CalledFn).second)
|
2011-03-21 03:51:13 +08:00
|
|
|
return false;
|
|
|
|
|
2011-03-22 11:21:01 +08:00
|
|
|
if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions))
|
2011-03-21 01:59:11 +08:00
|
|
|
return false;
|
|
|
|
} else if (isa<ReturnInst>(*I))
|
2012-02-09 22:26:06 +08:00
|
|
|
return true; // We're done.
|
|
|
|
else if (I->mayHaveSideEffects())
|
|
|
|
return false; // Destructor with side effects, bail.
|
2011-03-21 01:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalOpt::OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
|
|
|
|
/// Itanium C++ ABI p3.3.5:
|
|
|
|
///
|
|
|
|
/// After constructing a global (or local static) object, that will require
|
|
|
|
/// destruction on exit, a termination function is registered as follows:
|
|
|
|
///
|
|
|
|
/// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
|
|
|
|
///
|
|
|
|
/// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
|
|
|
|
/// call f(p) when DSO d is unloaded, before all such termination calls
|
|
|
|
/// registered before this one. It returns zero if registration is
|
2011-03-21 10:26:01 +08:00
|
|
|
/// successful, nonzero on failure.
|
2011-03-21 01:59:11 +08:00
|
|
|
|
|
|
|
// This pass will look for calls to __cxa_atexit where the function is trivial
|
|
|
|
// and remove them.
|
|
|
|
bool Changed = false;
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end();
|
|
|
|
I != E;) {
|
2011-03-21 04:21:33 +08:00
|
|
|
// We're only interested in calls. Theoretically, we could handle invoke
|
|
|
|
// instructions as well, but neither llvm-gcc nor clang generate invokes
|
|
|
|
// to __cxa_atexit.
|
2011-03-21 22:54:40 +08:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(*I++);
|
|
|
|
if (!CI)
|
2011-03-21 04:21:33 +08:00
|
|
|
continue;
|
|
|
|
|
2012-12-07 05:57:16 +08:00
|
|
|
Function *DtorFn =
|
2011-03-21 22:54:40 +08:00
|
|
|
dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
|
2011-03-21 01:59:11 +08:00
|
|
|
if (!DtorFn)
|
|
|
|
continue;
|
|
|
|
|
2011-03-21 04:16:43 +08:00
|
|
|
SmallPtrSet<const Function *, 8> CalledFunctions;
|
|
|
|
if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions))
|
2011-03-21 01:59:11 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Just remove the call.
|
2011-03-21 22:54:40 +08:00
|
|
|
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
|
|
|
|
CI->eraseFromParent();
|
2011-03-21 03:51:13 +08:00
|
|
|
|
2011-03-21 01:59:11 +08:00
|
|
|
++NumCXXDtorsRemoved;
|
|
|
|
|
|
|
|
Changed |= true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
bool GlobalOpt::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2015-03-05 02:43:29 +08:00
|
|
|
auto &DL = M.getDataLayout();
|
2015-01-15 18:41:28 +08:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2012-02-12 09:13:18 +08:00
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
bool LocalChange = true;
|
2004-10-09 04:59:28 +08:00
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2014-10-08 15:23:31 +08:00
|
|
|
NotDiscardableComdats.clear();
|
|
|
|
for (const GlobalVariable &GV : M.globals())
|
|
|
|
if (const Comdat *C = GV.getComdat())
|
|
|
|
if (!GV.isDiscardableIfUnused() || !GV.use_empty())
|
|
|
|
NotDiscardableComdats.insert(C);
|
|
|
|
for (Function &F : M)
|
|
|
|
if (const Comdat *C = F.getComdat())
|
|
|
|
if (!F.isDefTriviallyDead())
|
|
|
|
NotDiscardableComdats.insert(C);
|
|
|
|
for (GlobalAlias &GA : M.aliases())
|
|
|
|
if (const Comdat *C = GA.getComdat())
|
|
|
|
if (!GA.isDiscardableIfUnused() || !GA.use_empty())
|
|
|
|
NotDiscardableComdats.insert(C);
|
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
// Delete functions that are trivially dead, ccc -> fastcc
|
|
|
|
LocalChange |= OptimizeFunctions(M);
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
// Optimize global_ctors list.
|
2014-05-06 09:44:26 +08:00
|
|
|
LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) {
|
|
|
|
return EvaluateStaticConstructor(F, DL, TLI);
|
|
|
|
});
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
// Optimize non-address-taken globals.
|
|
|
|
LocalChange |= OptimizeGlobalVars(M);
|
2008-09-10 03:04:59 +08:00
|
|
|
|
|
|
|
// Resolve aliases, when possible.
|
2009-03-06 18:21:56 +08:00
|
|
|
LocalChange |= OptimizeGlobalAliases(M);
|
2011-03-21 01:59:11 +08:00
|
|
|
|
2013-05-15 05:52:44 +08:00
|
|
|
// Try to remove trivial global destructors if they are not removed
|
|
|
|
// already.
|
|
|
|
Function *CXAAtExitFn = FindCXAAtExit(M, TLI);
|
2011-03-21 01:59:11 +08:00
|
|
|
if (CXAAtExitFn)
|
|
|
|
LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
|
|
|
|
|
2008-09-10 03:04:59 +08:00
|
|
|
Changed |= LocalChange;
|
2004-10-07 12:16:33 +08:00
|
|
|
}
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2005-09-26 09:43:45 +08:00
|
|
|
// TODO: Move all global ctors functions to the end of the module for code
|
|
|
|
// layout.
|
2010-10-19 05:16:00 +08:00
|
|
|
|
2004-10-07 12:16:33 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2015-07-23 02:50:10 +08:00
|
|
|
|