[llvm-commits] [llvm] r77579 - in /llvm/trunk/lib: 'Twine' errors in Transforms/Utils/PromoteMemoryToRegister.cpp Transforms/Scalar/ScalarReplAggregates.cpp on Cygwin
Aaron Gray
aaronngray.lists at googlemail.com
Thu Jul 30 08:57:24 PDT 2009
2009/7/30 Daniel Dunbar <daniel at zuster.org>
> Author: ddunbar
> Date: Wed Jul 29 23:20:37 2009
> New Revision: 77579
>
> URL: http://llvm.org/viewvc/llvm-project?rev=77579&view=rev
> Log:
> Switch obvious clients to Twine instead of utostr (when they were already
> using
> a Twine, e.g., for names).
> - I am a little ambivalent about this; we don't want the string conversion
> of
> utostr, but using overload '+' mixed with string and integer arguments is
> sketchy. On the other hand, this particular usage is something of an
> idiom.
Daniel,
I am getting the following error on Cygwin using GCC 4.2.2 :-
~~~~~~~~~~~~~~~~~
llvm[3]: Compiling PromoteMemoryToRegister.cpp for Debug build
/home/ang/svn/llvm-coff/lib/Transforms/Utils/PromoteMemoryToRegister.cpp: In
mem
ber function 'bool<unnamed>::PromoteMem2Reg::QueuePhiNode(llvm::BasicBlock*,
uns
igned int, unsigned int&, llvm::SmallPtrSet<llvm::PHINode*, 16u>&)':
/home/ang/svn/llvm-coff/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:870:
er
ror: conversion from 'unsigned int' to 'const llvm::Twine' is ambiguous
/home/ang/svn/llvm-coff/include/llvm/ADT/Twine.h:270: note: candidates are:
llvm
::Twine::Twine(const int64_t&)
/home/ang/svn/llvm-coff/include/llvm/ADT/Twine.h:265: note:
llvm::Twine::Twine(
const uint64_t&)
/home/ang/svn/llvm-coff/include/llvm/ADT/Twine.h:260: note:
llvm::Twine::Twine(
const int32_t&)
/home/ang/svn/llvm-coff/include/llvm/ADT/Twine.h:255: note:
llvm::Twine::Twine(
const uint32_t&)
make[3]: ***
[/home/ang/build/llvm-coff/lib/Transforms/Utils/Debug/PromoteMemory
ToRegister.o] Error 1
make[3]: Leaving directory `/home/ang/build/llvm-coff/lib/Transforms/Utils'
make[2]: *** [Utils/.makeall] Error 2
make[2]: Leaving directory `/home/ang/build/llvm-coff/lib/Transforms'
make[1]: *** [Transforms/.makeall] Error 2
make[1]: Leaving directory `/home/ang/build/llvm-coff/lib'
make: *** [all] Error 1
~~~~~~~~~~~~~~~~~~
Its working fine on MSVC 2008, but Cygwin GCC 4.2.2 is failing to resolve
the typing here.
Transforms/Utils/PromoteMemoryToRegister.cpp
I had to hack it with :-
PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(),
Allocas[AllocaNo]->getName() + "." +
Twine((uint32_t)Version++),
BB->begin());
To get it to compile on Cygwin, maybe 'Version' should be a uint32_t
?
Transforms/Scalar/ScalarReplAggregates.cpp
Transforms/IPO/ArgumentPromotion.cpp
Transforms/IPO/GlobalOpt.cpp
Twine constructors and uint32_t types or casts.
I have done a patch but its a bit insane tab wise it seems to be removing
all tabs from blank lines.
Also the actual logic I have used is a "just get it working logic", so
beware :)
Aaron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20090730/d5e7d81e/attachment.html>
-------------- next part --------------
Index: lib/Transforms/Utils/PromoteMemoryToRegister.cpp
===================================================================
--- lib/Transforms/Utils/PromoteMemoryToRegister.cpp (revision 77596)
+++ lib/Transforms/Utils/PromoteMemoryToRegister.cpp (working copy)
@@ -104,21 +104,21 @@
class VISIBILITY_HIDDEN RenamePassData {
public:
typedef std::vector<Value *> ValVector;
-
+
RenamePassData() {}
RenamePassData(BasicBlock *B, BasicBlock *P,
const ValVector &V) : BB(B), Pred(P), Values(V) {}
BasicBlock *BB;
BasicBlock *Pred;
ValVector Values;
-
+
void swap(RenamePassData &RHS) {
std::swap(BB, RHS.BB);
std::swap(Pred, RHS.Pred);
Values.swap(RHS.Values);
}
};
-
+
/// LargeBlockInfo - This assigns and keeps a per-bb relative ordering of
/// load/store instructions in the block that directly load or store an alloca.
///
@@ -130,23 +130,23 @@
/// the start of the block.
DenseMap<const Instruction *, unsigned> InstNumbers;
public:
-
+
/// isInterestingInstruction - This code only looks at accesses to allocas.
static bool isInterestingInstruction(const Instruction *I) {
return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) ||
(isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1)));
}
-
+
/// getInstructionIndex - Get or calculate the index of the specified
/// instruction.
unsigned getInstructionIndex(const Instruction *I) {
assert(isInterestingInstruction(I) &&
"Not a load/store to/from an alloca?");
-
+
// If we already have this instruction number, return it.
DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I);
if (It != InstNumbers.end()) return It->second;
-
+
// Scan the whole block to get the instruction. This accumulates
// information for every interesting instruction in the block, in order to
// avoid gratuitus rescans.
@@ -157,15 +157,15 @@
if (isInterestingInstruction(BBI))
InstNumbers[BBI] = InstNo++;
It = InstNumbers.find(I);
-
+
assert(It != InstNumbers.end() && "Didn't insert instruction?");
return It->second;
}
-
+
void deleteValue(const Instruction *I) {
InstNumbers.erase(I);
}
-
+
void clear() {
InstNumbers.clear();
}
@@ -181,7 +181,7 @@
/// AST - An AliasSetTracker object to update. If null, don't update it.
///
AliasSetTracker *AST;
-
+
LLVMContext &Context;
/// AllocaLookup - Reverse mapping of Allocas.
@@ -191,11 +191,11 @@
/// NewPhiNodes - The PhiNodes we're adding.
///
DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*> NewPhiNodes;
-
+
/// PhiToAllocaMap - For each PHI node, keep track of which entry in Allocas
/// it corresponds to.
DenseMap<PHINode*, unsigned> PhiToAllocaMap;
-
+
/// PointerAllocaValues - If we are updating an AliasSetTracker, then for
/// each alloca that is of pointer type, we keep track of what to copyValue
/// to the inserted PHI nodes here.
@@ -227,7 +227,7 @@
I1 = II->getNormalDest()->begin();
return DT.properlyDominates(I1->getParent(), I2->getParent());
}
-
+
/// dominates - Return true if BB1 dominates BB2 using the DominatorTree.
///
bool dominates(BasicBlock *BB1, BasicBlock *BB2) const {
@@ -250,33 +250,33 @@
void DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum,
AllocaInfo &Info);
- void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
+ void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
const SmallPtrSet<BasicBlock*, 32> &DefBlocks,
SmallPtrSet<BasicBlock*, 32> &LiveInBlocks);
-
+
void RewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
LargeBlockInfo &LBI);
void PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
LargeBlockInfo &LBI);
-
+
void RenamePass(BasicBlock *BB, BasicBlock *Pred,
RenamePassData::ValVector &IncVals,
std::vector<RenamePassData> &Worklist);
bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version,
SmallPtrSet<PHINode*, 16> &InsertedPHINodes);
};
-
+
struct AllocaInfo {
std::vector<BasicBlock*> DefiningBlocks;
std::vector<BasicBlock*> UsingBlocks;
-
+
StoreInst *OnlyStore;
BasicBlock *OnlyBlock;
bool OnlyUsedInOneBlock;
-
+
Value *AllocaPointerVal;
-
+
void clear() {
DefiningBlocks.clear();
UsingBlocks.clear();
@@ -285,7 +285,7 @@
OnlyUsedInOneBlock = true;
AllocaPointerVal = 0;
}
-
+
/// AnalyzeAlloca - Scan the uses of the specified alloca, filling in our
/// ivars.
void AnalyzeAlloca(AllocaInst *AI) {
@@ -305,7 +305,7 @@
DI->eraseFromParent();
BC->eraseFromParent();
continue;
- }
+ }
else if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
// Remember the basic blocks which define new values for the alloca
DefiningBlocks.push_back(SI->getParent());
@@ -318,7 +318,7 @@
UsingBlocks.push_back(LI->getParent());
AllocaPointerVal = LI;
}
-
+
if (OnlyUsedInOneBlock) {
if (OnlyBlock == 0)
OnlyBlock = User->getParent();
@@ -357,7 +357,7 @@
++NumDeadAlloca;
continue;
}
-
+
// Calculate the set of read and write-locations for each alloca. This is
// analogous to finding the 'uses' and 'definitions' of each variable.
Info.AnalyzeAlloca(AI);
@@ -376,43 +376,43 @@
if (AST) AST->deleteValue(AI);
AI->eraseFromParent();
LBI.deleteValue(AI);
-
+
// The alloca has been processed, move on.
RemoveFromAllocasList(AllocaNum);
-
+
++NumSingleStore;
continue;
}
}
-
+
// If the alloca is only read and written in one basic block, just perform a
// linear sweep over the block to eliminate it.
if (Info.OnlyUsedInOneBlock) {
PromoteSingleBlockAlloca(AI, Info, LBI);
-
+
// Finally, after the scan, check to see if the stores are all that is
// left.
if (Info.UsingBlocks.empty()) {
-
+
// Remove the (now dead) stores and alloca.
while (!AI->use_empty()) {
StoreInst *SI = cast<StoreInst>(AI->use_back());
SI->eraseFromParent();
LBI.deleteValue(SI);
}
-
+
if (AST) AST->deleteValue(AI);
AI->eraseFromParent();
LBI.deleteValue(AI);
-
+
// The alloca has been processed, move on.
RemoveFromAllocasList(AllocaNum);
-
+
++NumLocalPromoted;
continue;
}
}
-
+
// If we haven't computed a numbering for the BB's in the function, do so
// now.
if (BBNumbers.empty()) {
@@ -425,7 +425,7 @@
// stored into the alloca.
if (AST)
PointerAllocaValues[AllocaNum] = Info.AllocaPointerVal;
-
+
// Keep the reverse mapping of the 'Allocas' array for the rename pass.
AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
@@ -440,8 +440,8 @@
return; // All of the allocas must have been trivial!
LBI.clear();
-
-
+
+
// Set the incoming values for the basic block to be null values for all of
// the alloca's. We do this in case there is a load of a value that has not
// been stored yet. In this case, it will get this null value.
@@ -462,7 +462,7 @@
// RenamePass may add new worklist entries.
RenamePass(RPD.BB, RPD.Pred, RPD.Values, RenamePassWorkList);
}
-
+
// The renamer uses the Visited set to avoid infinite loops. Clear it now.
Visited.clear();
@@ -480,7 +480,7 @@
A->eraseFromParent();
}
-
+
// Loop over all of the PHI nodes and see if there are any that we can get
// rid of because they merge all of the same incoming values. This can
// happen due to undef values coming into the PHI nodes. This process is
@@ -488,11 +488,11 @@
bool EliminatedAPHI = true;
while (EliminatedAPHI) {
EliminatedAPHI = false;
-
+
for (DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*>::iterator I =
NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E;) {
PHINode *PN = I->second;
-
+
// If this PHI node merges one value and/or undefs, get the value.
if (Value *V = PN->hasConstantValue(true)) {
if (!isa<Instruction>(V) ||
@@ -509,7 +509,7 @@
++I;
}
}
-
+
// At this point, the renamer has added entries to PHI nodes for all reachable
// code. Unfortunately, there may be unreachable blocks which the renamer
// hasn't traversed. If this is the case, the PHI nodes may not
@@ -533,12 +533,12 @@
// Get the preds for BB.
SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));
-
+
// Ok, now we know that all of the PHI nodes are missing entries for some
// basic blocks. Start by sorting the incoming predecessors for efficient
// access.
std::sort(Preds.begin(), Preds.end());
-
+
// Now we loop through all BB's which have entries in SomePHI and remove
// them from the Preds list.
for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
@@ -566,7 +566,7 @@
SomePHI->addIncoming(UndefVal, Preds[pred]);
}
}
-
+
NewPhiNodes.clear();
}
@@ -576,30 +576,30 @@
/// PHI nodes into blocks which don't lead to uses (thus, the inserted phi nodes
/// would be dead).
void PromoteMem2Reg::
-ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
+ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
const SmallPtrSet<BasicBlock*, 32> &DefBlocks,
SmallPtrSet<BasicBlock*, 32> &LiveInBlocks) {
-
+
// To determine liveness, we must iterate through the predecessors of blocks
// where the def is live. Blocks are added to the worklist if we need to
// check their predecessors. Start with all the using blocks.
SmallVector<BasicBlock*, 64> LiveInBlockWorklist;
- LiveInBlockWorklist.insert(LiveInBlockWorklist.end(),
+ LiveInBlockWorklist.insert(LiveInBlockWorklist.end(),
Info.UsingBlocks.begin(), Info.UsingBlocks.end());
-
+
// If any of the using blocks is also a definition block, check to see if the
// definition occurs before or after the use. If it happens before the use,
// the value isn't really live-in.
for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
BasicBlock *BB = LiveInBlockWorklist[i];
if (!DefBlocks.count(BB)) continue;
-
+
// Okay, this is a block that both uses and defines the value. If the first
// reference to the alloca is a def (store), then we know it isn't live-in.
for (BasicBlock::iterator I = BB->begin(); ; ++I) {
if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
if (SI->getOperand(1) != AI) continue;
-
+
// We found a store to the alloca before a load. The alloca is not
// actually live-in here.
LiveInBlockWorklist[i] = LiveInBlockWorklist.back();
@@ -608,34 +608,34 @@
break;
} else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
if (LI->getOperand(0) != AI) continue;
-
+
// Okay, we found a load before a store to the alloca. It is actually
// live into this block.
break;
}
}
}
-
+
// Now that we have a set of blocks where the phi is live-in, recursively add
// their predecessors until we find the full region the value is live.
while (!LiveInBlockWorklist.empty()) {
BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
-
+
// The block really is live in here, insert it into the set. If already in
// the set, then it has already been processed.
if (!LiveInBlocks.insert(BB))
continue;
-
+
// Since the value is live into BB, it is either defined in a predecessor or
// live into it to. Add the preds to the worklist unless they are a
// defining block.
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
BasicBlock *P = *PI;
-
+
// The value is not live into a predecessor if it defines the value.
if (DefBlocks.count(P))
continue;
-
+
// Otherwise it is, add to the worklist.
LiveInBlockWorklist.push_back(P);
}
@@ -666,13 +666,13 @@
while (!Info.DefiningBlocks.empty()) {
BasicBlock *BB = Info.DefiningBlocks.back();
Info.DefiningBlocks.pop_back();
-
+
// Look up the DF for this write, add it to defining blocks.
DominanceFrontier::const_iterator it = DF.find(BB);
if (it == DF.end()) continue;
-
+
const DominanceFrontier::DomSetType &S = it->second;
-
+
// In theory we don't need the indirection through the DFBlocks vector.
// In practice, the order of calling QueuePhiNode would depend on the
// (unspecified) ordering of basic blocks in the dominance frontier,
@@ -684,14 +684,14 @@
// bother processing it.
if (!LiveInBlocks.count(*P))
continue;
-
+
DFBlocks.push_back(std::make_pair(BBNumbers[*P], *P));
}
-
+
// Sort by which the block ordering in the function.
if (DFBlocks.size() > 1)
std::sort(DFBlocks.begin(), DFBlocks.end());
-
+
for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i) {
BasicBlock *BB = DFBlocks[i].second;
if (QueuePhiNode(BB, AllocaNum, CurrentVersion, InsertedPHINodes))
@@ -714,7 +714,7 @@
// Clear out UsingBlocks. We will reconstruct it here if needed.
Info.UsingBlocks.clear();
-
+
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E; ) {
Instruction *UserInst = cast<Instruction>(*UI++);
if (!isa<LoadInst>(UserInst)) {
@@ -722,7 +722,7 @@
continue;
}
LoadInst *LI = cast<LoadInst>(UserInst);
-
+
// Okay, if we have a load from the alloca, we want to replace it with the
// only value stored to the alloca. We can do this if the value is
// dominated by the store. If not, we use the rest of the mem2reg machinery
@@ -740,7 +740,7 @@
Info.UsingBlocks.push_back(StoreBB);
continue;
}
-
+
} else if (LI->getParent() != StoreBB &&
!dominates(StoreBB, LI->getParent())) {
// If the load and store are in different blocks, use BB dominance to
@@ -750,7 +750,7 @@
continue;
}
}
-
+
// Otherwise, we *can* safely rewrite this load.
LI->replaceAllUsesWith(OnlyStore->getOperand(0));
if (AST && isa<PointerType>(LI->getType()))
@@ -790,22 +790,22 @@
// this code is optimized assuming that large blocks happen. This does not
// significantly pessimize the small block case. This uses LargeBlockInfo to
// make it efficient to get the index of various operations in the block.
-
+
// Clear out UsingBlocks. We will reconstruct it here if needed.
Info.UsingBlocks.clear();
-
+
// Walk the use-def list of the alloca, getting the locations of all stores.
typedef SmallVector<std::pair<unsigned, StoreInst*>, 64> StoresByIndexTy;
StoresByIndexTy StoresByIndex;
-
+
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
- UI != E; ++UI)
+ UI != E; ++UI)
if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
// If there are no stores to the alloca, just replace any loads with undef.
if (StoresByIndex.empty()) {
- for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
+ for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
LI->replaceAllUsesWith(Context.getUndef(LI->getType()));
if (AST && isa<PointerType>(LI->getType()))
@@ -815,32 +815,32 @@
}
return;
}
-
+
// Sort the stores by their index, making it efficient to do a lookup with a
// binary search.
std::sort(StoresByIndex.begin(), StoresByIndex.end());
-
+
// Walk all of the loads from this alloca, replacing them with the nearest
// store above them, if any.
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
LoadInst *LI = dyn_cast<LoadInst>(*UI++);
if (!LI) continue;
-
+
unsigned LoadIdx = LBI.getInstructionIndex(LI);
-
- // Find the nearest store that has a lower than this load.
- StoresByIndexTy::iterator I =
+
+ // Find the nearest store that has a lower than this load.
+ StoresByIndexTy::iterator I =
std::lower_bound(StoresByIndex.begin(), StoresByIndex.end(),
std::pair<unsigned, StoreInst*>(LoadIdx, 0),
StoreIndexSearchPredicate());
-
+
// If there is no store before this load, then we can't promote this load.
if (I == StoresByIndex.begin()) {
// Can't handle this load, bail out.
Info.UsingBlocks.push_back(LI->getParent());
continue;
}
-
+
// Otherwise, there was a store before this load, the load takes its value.
--I;
LI->replaceAllUsesWith(I->second->getOperand(0));
@@ -867,12 +867,12 @@
// Create a PhiNode using the dereferenced type... and add the phi-node to the
// BasicBlock.
PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(),
- Allocas[AllocaNo]->getName() + "." + Version++,
+ Allocas[AllocaNo]->getName() + "." + Twine((uint32_t)Version++),
BB->begin());
++NumPHIInsert;
PhiToAllocaMap[PN] = AllocaNo;
PN->reserveOperandSpace(getNumPreds(BB));
-
+
InsertedPHINodes.insert(PN);
if (AST && isa<PointerType>(PN->getType()))
@@ -902,36 +902,36 @@
// inserted by this pass of mem2reg will have the same number of incoming
// operands so far. Remember this count.
unsigned NewPHINumOperands = APN->getNumOperands();
-
+
unsigned NumEdges = 0;
for (succ_iterator I = succ_begin(Pred), E = succ_end(Pred); I != E; ++I)
if (*I == BB)
++NumEdges;
assert(NumEdges && "Must be at least one edge from Pred to BB!");
-
+
// Add entries for all the phis.
BasicBlock::iterator PNI = BB->begin();
do {
unsigned AllocaNo = PhiToAllocaMap[APN];
-
+
// Add N incoming values to the PHI node.
for (unsigned i = 0; i != NumEdges; ++i)
APN->addIncoming(IncomingVals[AllocaNo], Pred);
-
+
// The currently active variable for this block is now the PHI.
IncomingVals[AllocaNo] = APN;
-
+
// Get the next phi node.
++PNI;
APN = dyn_cast<PHINode>(PNI);
if (APN == 0) break;
-
+
// Verify that it is missing entries. If not, it is not being inserted
// by this mem2reg invocation so we want to ignore it.
} while (APN->getNumOperands() == NewPHINumOperands);
}
}
-
+
// Don't revisit blocks.
if (!Visited.insert(BB)) return;
@@ -941,7 +941,7 @@
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand());
if (!Src) continue;
-
+
std::map<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src);
if (AI == AllocaLookup.end()) continue;
@@ -957,11 +957,11 @@
// value
AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand());
if (!Dest) continue;
-
+
std::map<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
if (ai == AllocaLookup.end())
continue;
-
+
// what value were we writing?
IncomingVals[ai->second] = SI->getOperand(0);
BB->getInstList().erase(SI);
Index: lib/Transforms/Scalar/ScalarReplAggregates.cpp
===================================================================
--- lib/Transforms/Scalar/ScalarReplAggregates.cpp (revision 77596)
+++ lib/Transforms/Scalar/ScalarReplAggregates.cpp (working copy)
@@ -74,18 +74,18 @@
private:
TargetData *TD;
-
+
/// AllocaInfo - When analyzing uses of an alloca instruction, this captures
/// information about the uses. All these fields are initialized to false
/// and set to true when something is learned.
struct AllocaInfo {
/// isUnsafe - This is set to true if the alloca cannot be SROA'd.
bool isUnsafe : 1;
-
+
/// needsCleanup - This is set to true if there is some use of the alloca
/// that requires cleanup.
bool needsCleanup : 1;
-
+
/// isMemCpySrc - This is true if this aggregate is memcpy'd from.
bool isMemCpySrc : 1;
@@ -93,10 +93,10 @@
bool isMemCpyDst : 1;
AllocaInfo()
- : isUnsafe(false), needsCleanup(false),
+ : isUnsafe(false), needsCleanup(false),
isMemCpySrc(false), isMemCpyDst(false) {}
};
-
+
unsigned SRThreshold;
void MarkUnsafe(AllocaInfo &I) { I.isUnsafe = true; }
@@ -111,16 +111,16 @@
unsigned OpNo, AllocaInfo &Info);
void isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocationInst *AI,
AllocaInfo &Info);
-
- void DoScalarReplacement(AllocationInst *AI,
+
+ void DoScalarReplacement(AllocationInst *AI,
std::vector<AllocationInst*> &WorkList);
void CleanupGEP(GetElementPtrInst *GEP);
void CleanupAllocaUsers(AllocationInst *AI);
AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
-
+
void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI,
SmallVector<AllocaInst*, 32> &NewElts);
-
+
void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
AllocationInst *AI,
SmallVector<AllocaInst*, 32> &NewElts);
@@ -128,7 +128,7 @@
SmallVector<AllocaInst*, 32> &NewElts);
void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocationInst *AI,
SmallVector<AllocaInst*, 32> &NewElts);
-
+
bool CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy,
bool &SawVec, uint64_t Offset, unsigned AllocaSize);
void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset);
@@ -144,14 +144,14 @@
static RegisterPass<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
// Public interface to the ScalarReplAggregates pass
-FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) {
+FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) {
return new SROA(Threshold);
}
bool SROA::runOnFunction(Function &F) {
TD = &getAnalysis<TargetData>();
-
+
bool Changed = performPromotion(F);
while (1) {
bool LocalChange = performScalarRepl(F);
@@ -220,7 +220,7 @@
while (!WorkList.empty()) {
AllocationInst *AI = WorkList.back();
WorkList.pop_back();
-
+
// Handle dead allocas trivially. These can be formed by SROA'ing arrays
// with unused elements.
if (AI->use_empty()) {
@@ -231,7 +231,7 @@
// If this alloca is impossible for us to promote, reject it early.
if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized())
continue;
-
+
// Check to see if this allocation is only modified by a memcpy/memmove from
// a constant global. If this is the case, we can change all users to use
// the constant global instead. This is commonly produced by the CFE by
@@ -248,7 +248,7 @@
Changed = true;
continue;
}
-
+
// Check to see if we can perform the core SROA transformation. We cannot
// transform the allocation instruction if it is an array allocation
// (allocations OF arrays are ok though), and an allocation of a scalar
@@ -257,7 +257,7 @@
// Do not promote any struct whose size is too big.
if (AllocaSize > SRThreshold) continue;
-
+
if ((isa<StructType>(AI->getAllocatedType()) ||
isa<ArrayType>(AI->getAllocatedType())) &&
// Do not promote any struct into more than "32" separate vars.
@@ -287,7 +287,7 @@
bool IsNotTrivial = false;
const Type *VectorTy = 0;
bool HadAVector = false;
- if (CanConvertToScalar(AI, IsNotTrivial, VectorTy, HadAVector,
+ if (CanConvertToScalar(AI, IsNotTrivial, VectorTy, HadAVector,
0, unsigned(AllocaSize)) && IsNotTrivial) {
AllocaInst *NewAI;
// If we were able to find a vector type that can handle this with
@@ -298,13 +298,13 @@
// involved, then we probably really do have a union of vector/array.
if (VectorTy && isa<VectorType>(VectorTy) && HadAVector) {
DOUT << "CONVERT TO VECTOR: " << *AI << " TYPE = " << *VectorTy <<"\n";
-
+
// Create and insert the vector alloca.
NewAI = new AllocaInst(VectorTy, 0, "", AI->getParent()->begin());
ConvertUsesToScalar(AI, NewAI, 0);
} else {
DOUT << "CONVERT TO SCALAR INTEGER: " << *AI << "\n";
-
+
// Create and insert the integer alloca.
const Type *NewTy = IntegerType::get(AllocaSize*8);
NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin());
@@ -316,7 +316,7 @@
Changed = true;
continue;
}
-
+
// Otherwise, couldn't process this alloca.
}
@@ -325,17 +325,17 @@
/// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl
/// predicate, do SROA now.
-void SROA::DoScalarReplacement(AllocationInst *AI,
+void SROA::DoScalarReplacement(AllocationInst *AI,
std::vector<AllocationInst*> &WorkList) {
DOUT << "Found inst to SROA: " << *AI;
SmallVector<AllocaInst*, 32> ElementAllocas;
LLVMContext &Context = AI->getContext();
if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
ElementAllocas.reserve(ST->getNumContainedTypes());
- for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
- AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
+ for (uint32_t i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
+ AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
AI->getAlignment(),
- AI->getName() + "." + i, AI);
+ AI->getName() + "." + Twine(i), AI);
ElementAllocas.push_back(NA);
WorkList.push_back(NA); // Add to worklist for recursive processing
}
@@ -343,9 +343,9 @@
const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
ElementAllocas.reserve(AT->getNumElements());
const Type *ElTy = AT->getElementType();
- for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
+ for (uint32_t i = 0, e = AT->getNumElements(); i != e; ++i) {
AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(),
- AI->getName() + "." + i, AI);
+ AI->getName() + "." + Twine(i), AI);
ElementAllocas.push_back(NA);
WorkList.push_back(NA); // Add to worklist for recursive processing
}
@@ -361,14 +361,14 @@
BCInst->eraseFromParent();
continue;
}
-
+
// Replace:
// %res = load { i32, i32 }* %alloc
// with:
// %load.0 = load i32* %alloc.0
- // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0
+ // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0
// %load.1 = load i32* %alloc.1
- // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1
+ // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1
// (Also works for arrays instead of structs)
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
Value *Insert = Context.getUndef(LI->getType());
@@ -384,9 +384,9 @@
// Replace:
// store { i32, i32 } %val, { i32, i32 }* %alloc
// with:
- // %val.0 = extractvalue { i32, i32 } %val, 0
+ // %val.0 = extractvalue { i32, i32 } %val, 0
// store i32 %val.0, i32* %alloc.0
- // %val.1 = extractvalue { i32, i32 } %val, 1
+ // %val.1 = extractvalue { i32, i32 } %val, 1
// store i32 %val.1, i32* %alloc.1
// (Also works for arrays instead of structs)
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
@@ -398,7 +398,7 @@
SI->eraseFromParent();
continue;
}
-
+
GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User);
// We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
unsigned Idx =
@@ -424,7 +424,7 @@
NewArgs.end(), "", GEPI);
RepValue->takeName(GEPI);
}
-
+
// If this GEP is to the start of the aggregate, check for memcpys.
if (Idx == 0 && GEPI->hasAllZeroIndices())
RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas);
@@ -464,7 +464,7 @@
!cast<ConstantInt>(GEP->getOperand(1))->isZero())
// Using pointer arithmetic to navigate the array.
return MarkUnsafe(Info);
-
+
if (AreAllZeroIndices)
AreAllZeroIndices = GEP->hasAllZeroIndices();
}
@@ -523,7 +523,7 @@
if (StoreInst *SI = dyn_cast<StoreInst>(User))
if (!SI->isVolatile() && SI->getOperand(0) != AI)
return;// Store is ok if storing INTO the pointer, not storing the pointer
-
+
GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User);
if (GEPI == 0)
return MarkUnsafe(Info);
@@ -540,14 +540,14 @@
if (I == E) return MarkUnsafe(Info); // ran out of GEP indices??
bool IsAllZeroIndices = true;
-
+
// If the first index is a non-constant index into an array, see if we can
// handle it as a special case.
if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
if (!isa<ConstantInt>(I.getOperand())) {
IsAllZeroIndices = 0;
uint64_t NumElements = AT->getNumElements();
-
+
// If this is an array index and the index is not constant, we cannot
// promote... that is unless the array has exactly one or two elements in
// it, in which case we CAN promote it, but we have to canonicalize this
@@ -560,26 +560,26 @@
return MarkUnsafe(Info);
}
}
-
+
// Walk through the GEP type indices, checking the types that this indexes
// into.
for (; I != E; ++I) {
// Ignore struct elements, no extra checking needed for these.
if (isa<StructType>(*I))
continue;
-
+
ConstantInt *IdxVal = dyn_cast<ConstantInt>(I.getOperand());
if (!IdxVal) return MarkUnsafe(Info);
// Are all indices still zero?
IsAllZeroIndices &= IdxVal->isZero();
-
+
if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
// This GEP indexes an array. Verify that this is an in-range constant
// integer. Specifically, 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]. Because of this, we have to reject SROA
- // of any accesses into structs where any of the components are variables.
+ // of any accesses into structs where any of the components are variables.
if (IdxVal->getZExtValue() >= AT->getNumElements())
return MarkUnsafe(Info);
} else if (const VectorType *VT = dyn_cast<VectorType>(*I)) {
@@ -587,7 +587,7 @@
return MarkUnsafe(Info);
}
}
-
+
// If there are any non-simple uses of this getelementptr, make sure to reject
// them.
return isSafeElementUse(GEPI, IsAllZeroIndices, AI, Info);
@@ -601,16 +601,16 @@
// If not constant length, give up.
ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
if (!Length) return MarkUnsafe(Info);
-
+
// If not the whole aggregate, give up.
if (Length->getZExtValue() !=
TD->getTypeAllocSize(AI->getType()->getElementType()))
return MarkUnsafe(Info);
-
+
// We only know about memcpy/memset/memmove.
if (!isa<MemIntrinsic>(MI))
return MarkUnsafe(Info);
-
+
// Otherwise, we can transform it. Determine whether this is a memcpy/set
// into or out of the aggregate.
if (OpNo == 1)
@@ -622,7 +622,7 @@
}
/// isSafeUseOfBitCastedAllocation - Return true if all users of this bitcast
-/// are
+/// are
void SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocationInst *AI,
AllocaInfo &Info) {
for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end();
@@ -634,7 +634,7 @@
} else if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
if (SI->isVolatile())
return MarkUnsafe(Info);
-
+
// If storing the entire alloca in one chunk through a bitcasted pointer
// to integer, we can transform it. This happens (for example) when you
// cast a {i32,i32}* to i64* and store through it. This is similar to the
@@ -698,7 +698,7 @@
RewriteMemIntrinUserOfAlloca(MI, BCInst, AI, NewElts);
continue;
}
-
+
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
// If this is a store of the entire alloca from an integer, rewrite it.
RewriteStoreUserOfWholeAlloca(SI, AI, NewElts);
@@ -710,7 +710,7 @@
RewriteLoadUserOfWholeAlloca(LI, AI, NewElts);
continue;
}
-
+
// Otherwise it must be some other user of a gep of the first pointer. Just
// leave these alone.
continue;
@@ -722,7 +722,7 @@
void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
AllocationInst *AI,
SmallVector<AllocaInst*, 32> &NewElts) {
-
+
// If this is a memcpy/memmove, construct the other pointer as the
// appropriate type. The "Other" pointer is the pointer that goes to memory
// that doesn't have anything to do with the alloca that we are promoting. For
@@ -738,7 +738,7 @@
OtherPtr = MTI->getRawDest();
}
}
-
+
// If there is an other pointer, we want to convert it to the same pointer
// type as AI has, so we can GEP through it safely.
if (OtherPtr) {
@@ -749,35 +749,34 @@
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(OtherPtr))
if (GEP->hasAllZeroIndices())
OtherPtr = GEP->getOperand(0);
-
+
if (ConstantExpr *BCE = dyn_cast<ConstantExpr>(OtherPtr))
if (BCE->getOpcode() == Instruction::BitCast)
OtherPtr = BCE->getOperand(0);
-
+
// If the pointer is not the right type, insert a bitcast to the right
// type.
if (OtherPtr->getType() != AI->getType())
OtherPtr = new BitCastInst(OtherPtr, AI->getType(), OtherPtr->getName(),
MI);
}
-
+
// Process each element of the aggregate.
Value *TheFn = MI->getOperand(0);
const Type *BytePtrTy = MI->getRawDest()->getType();
bool SROADest = MI->getRawDest() == BCInst;
-
+
Constant *Zero = Context.getNullValue(Type::Int32Ty);
- for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
+ for (uint32_t i = 0, e = NewElts.size(); i != e; ++i) {
// If this is a memcpy/memmove, emit a GEP of the other element address.
Value *OtherElt = 0;
unsigned OtherEltAlign = MemAlignment;
-
+
if (OtherPtr) {
Value *Idx[2] = { Zero, ConstantInt::get(Type::Int32Ty, i) };
OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2,
- OtherPtr->getNameStr()+"."+i,
- MI);
+ OtherPtr->getNameStr()+"."+Twine(i), MI);
uint64_t EltOffset;
const PointerType *OtherPtrTy = cast<PointerType>(OtherPtr->getType());
if (const StructType *ST =
@@ -788,7 +787,7 @@
cast<SequentialType>(OtherPtr->getType())->getElementType();
EltOffset = TD->getTypeAllocSize(EltTy)*i;
}
-
+
// The alignment of the other pointer is the guaranteed alignment of the
// element, which is affected by both the known alignment of the whole
// mem intrinsic and the alignment of the element. If the alignment of
@@ -796,10 +795,10 @@
// known alignment is just 4 bytes.
OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset);
}
-
+
Value *EltPtr = NewElts[i];
const Type *EltTy = cast<PointerType>(EltPtr->getType())->getElementType();
-
+
// If we got down to a scalar, insert a load or store as appropriate.
if (EltTy->isSingleValueType()) {
if (isa<MemTransferInst>(MI)) {
@@ -815,7 +814,7 @@
continue;
}
assert(isa<MemSetInst>(MI));
-
+
// If the stored element is zero (common case), just store a null
// constant.
Constant *StoreVal;
@@ -835,7 +834,7 @@
TotalVal = TotalVal.shl(8);
TotalVal |= OneVal;
}
-
+
// Convert the integer value to the appropriate type.
StoreVal = ConstantInt::get(Context, TotalVal);
if (isa<PointerType>(ValTy))
@@ -843,7 +842,7 @@
else if (ValTy->isFloatingPoint())
StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy);
assert(StoreVal->getType() == ValTy && "Type mismatch!");
-
+
// If the requested value was a vector constant, create it.
if (EltTy != ValTy) {
unsigned NumElts = cast<VectorType>(ValTy)->getNumElements();
@@ -857,18 +856,18 @@
// Otherwise, if we're storing a byte variable, use a memset call for
// this element.
}
-
+
// Cast the element pointer to BytePtrTy.
if (EltPtr->getType() != BytePtrTy)
EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getNameStr(), MI);
-
- // Cast the other pointer (if we have one) to BytePtrTy.
+
+ // Cast the other pointer (if we have one) to BytePtrTy.
if (OtherElt && OtherElt->getType() != BytePtrTy)
OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(),
MI);
-
+
unsigned EltSize = TD->getTypeAllocSize(EltTy);
-
+
// Finally, insert the meminst for this element.
if (isa<MemTransferInst>(MI)) {
Value *Ops[] = {
@@ -902,7 +901,7 @@
Value *SrcVal = SI->getOperand(0);
const Type *AllocaEltTy = AI->getType()->getElementType();
uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy);
-
+
// If this isn't a store of an integer to the whole alloca, it may be a store
// to the first element. Just ignore the store in this case and normal SROA
// will handle it.
@@ -920,28 +919,28 @@
// have different ways to compute the element offset.
if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) {
const StructLayout *Layout = TD->getStructLayout(EltSTy);
-
+
for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
// Get the number of bits to shift SrcVal to get the value.
const Type *FieldTy = EltSTy->getElementType(i);
uint64_t Shift = Layout->getElementOffsetInBits(i);
-
+
if (TD->isBigEndian())
Shift = AllocaSizeBits-Shift-TD->getTypeAllocSizeInBits(FieldTy);
-
+
Value *EltVal = SrcVal;
if (Shift) {
Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift);
EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
"sroa.store.elt", SI);
}
-
+
// Truncate down to an integer of the right size.
uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy);
-
+
// Ignore zero sized fields like {}, they obviously contain no data.
if (FieldSizeBits == 0) continue;
-
+
if (FieldSizeBits != AllocaSizeBits)
EltVal = new TruncInst(EltVal,
IntegerType::get(FieldSizeBits), "", SI);
@@ -959,7 +958,7 @@
}
new StoreInst(EltVal, DestField, SI);
}
-
+
} else {
const ArrayType *ATy = cast<ArrayType>(AllocaEltTy);
const Type *ArrayEltTy = ATy->getElementType();
@@ -967,26 +966,26 @@
uint64_t ElementSizeBits = TD->getTypeSizeInBits(ArrayEltTy);
uint64_t Shift;
-
+
if (TD->isBigEndian())
Shift = AllocaSizeBits-ElementOffset;
- else
+ else
Shift = 0;
-
+
for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
// Ignore zero sized fields like {}, they obviously contain no data.
if (ElementSizeBits == 0) continue;
-
+
Value *EltVal = SrcVal;
if (Shift) {
Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift);
EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
"sroa.store.elt", SI);
}
-
+
// Truncate down to an integer of the right size.
if (ElementSizeBits != AllocaSizeBits)
- EltVal = new TruncInst(EltVal,
+ EltVal = new TruncInst(EltVal,
IntegerType::get(ElementSizeBits),"",SI);
Value *DestField = NewElts[i];
if (EltVal->getType() == ArrayEltTy) {
@@ -1001,14 +1000,14 @@
"", SI);
}
new StoreInst(EltVal, DestField, SI);
-
+
if (TD->isBigEndian())
Shift -= ElementOffset;
- else
+ else
Shift += ElementOffset;
}
}
-
+
SI->eraseFromParent();
}
@@ -1020,16 +1019,16 @@
// and form the result value.
const Type *AllocaEltTy = AI->getType()->getElementType();
uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy);
-
+
// If this isn't a load of the whole alloca to an integer, it may be a load
// of the first element. Just ignore the load in this case and normal SROA
// will handle it.
if (!isa<IntegerType>(LI->getType()) ||
TD->getTypeAllocSizeInBits(LI->getType()) != AllocaSizeBits)
return;
-
+
DOUT << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << *LI;
-
+
// There are two forms here: AI could be an array or struct. Both cases
// have different ways to compute the element offset.
const StructLayout *Layout = 0;
@@ -1039,13 +1038,13 @@
} else {
const Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType();
ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy);
- }
-
+ }
+
LLVMContext &Context = LI->getContext();
-
- Value *ResultVal =
+
+ Value *ResultVal =
Context.getNullValue(IntegerType::get(AllocaSizeBits));
-
+
for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
// Load the value from the alloca. If the NewElt is an aggregate, cast
// the pointer to an integer of the same size before doing the load.
@@ -1053,10 +1052,10 @@
const Type *FieldTy =
cast<PointerType>(SrcField->getType())->getElementType();
uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy);
-
+
// Ignore zero sized fields like {}, they obviously contain no data.
if (FieldSizeBits == 0) continue;
-
+
const IntegerType *FieldIntTy = IntegerType::get(FieldSizeBits);
if (!isa<IntegerType>(FieldTy) && !FieldTy->isFloatingPoint() &&
!isa<VectorType>(FieldTy))
@@ -1074,17 +1073,17 @@
// we can shift and insert it.
if (SrcField->getType() != ResultVal->getType())
SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI);
-
+
// Determine the number of bits to shift SrcField.
uint64_t Shift;
if (Layout) // Struct case.
Shift = Layout->getElementOffsetInBits(i);
else // Array case.
Shift = i*ArrayEltBitOffset;
-
+
if (TD->isBigEndian())
Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth();
-
+
if (Shift) {
Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift);
SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI);
@@ -1151,7 +1150,7 @@
// Loop over the use list of the alloca. We can only transform it if all of
// the users are safe to transform.
AllocaInfo Info;
-
+
for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
I != E; ++I) {
isSafeUseOfAllocation(cast<Instruction>(*I), AI, Info);
@@ -1160,7 +1159,7 @@
return 0;
}
}
-
+
// Okay, we know all the users are promotable. If the aggregate is a memcpy
// source and destination, we have to be careful. In particular, the memcpy
// could be moving around elements that live in structure padding of the LLVM
@@ -1179,13 +1178,13 @@
void SROA::CleanupGEP(GetElementPtrInst *GEPI) {
gep_type_iterator I = gep_type_begin(GEPI);
++I;
-
+
const ArrayType *AT = dyn_cast<ArrayType>(*I);
- if (!AT)
+ if (!AT)
return;
uint64_t NumElements = AT->getNumElements();
-
+
if (isa<ConstantInt>(I.getOperand()))
return;
@@ -1194,12 +1193,12 @@
if (NumElements == 1) {
GEPI->setOperand(2, Context.getNullValue(Type::Int32Ty));
return;
- }
-
+ }
+
assert(NumElements == 2 && "Unhandled case!");
// All users of the GEP must be loads. At each use of the GEP, insert
// two loads of the appropriate indexed GEP and select between them.
- Value *IsOne = new ICmpInst(GEPI, ICmpInst::ICMP_NE, I.getOperand(),
+ Value *IsOne = new ICmpInst(GEPI, ICmpInst::ICMP_NE, I.getOperand(),
Context.getNullValue(I.getOperand()->getType()),
"isone");
// Insert the new GEP instructions, which are properly indexed.
@@ -1291,7 +1290,7 @@
unsigned EltSize = In->getPrimitiveSizeInBits()/8;
if (Offset % EltSize == 0 &&
AllocaSize % EltSize == 0 &&
- (VecTy == 0 ||
+ (VecTy == 0 ||
cast<VectorType>(VecTy)->getElementType()
->getPrimitiveSizeInBits()/8 == EltSize)) {
if (VecTy == 0)
@@ -1300,7 +1299,7 @@
}
}
}
-
+
// Otherwise, we have a case that we can't handle with an optimized vector
// form. We can still turn this into a large integer.
VecTy = Type::VoidTy;
@@ -1321,7 +1320,7 @@
unsigned AllocaSize) {
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) {
Instruction *User = cast<Instruction>(*UI);
-
+
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
// Don't break volatile loads.
if (LI->isVolatile())
@@ -1331,7 +1330,7 @@
SawVec |= isa<VectorType>(LI->getType());
continue;
}
-
+
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
// Storing the pointer, not into the value?
if (SI->getOperand(0) == V || SI->isVolatile()) return 0;
@@ -1340,7 +1339,7 @@
SawVec |= isa<VectorType>(SI->getOperand(0)->getType());
continue;
}
-
+
if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
if (!CanConvertToScalar(BCI, IsNotTrivial, VecTy, SawVec, Offset,
AllocaSize))
@@ -1353,7 +1352,7 @@
// If this is a GEP with a variable indices, we can't handle it.
if (!GEP->hasAllConstantIndices())
return false;
-
+
// Compute the offset that this GEP adds to the pointer.
SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end());
uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(),
@@ -1386,7 +1385,7 @@
continue;
}
}
-
+
// Ignore dbg intrinsic.
if (isa<DbgInfoIntrinsic>(User))
continue;
@@ -1394,7 +1393,7 @@
// Otherwise, we cannot handle this!
return false;
}
-
+
return true;
}
@@ -1425,9 +1424,9 @@
GEP->eraseFromParent();
continue;
}
-
+
IRBuilder<> Builder(User->getParent(), User);
-
+
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
// The load is a bit extract from NewAI shifted right by Offset bits.
Value *LoadedVal = Builder.CreateLoad(NewAI, "tmp");
@@ -1437,7 +1436,7 @@
LI->eraseFromParent();
continue;
}
-
+
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
assert(SI->getOperand(0) != Ptr && "Consistency error!");
// FIXME: Remove once builder has Twine API.
@@ -1448,7 +1447,7 @@
SI->eraseFromParent();
continue;
}
-
+
// If this is a constant sized memset of a constant value (e.g. 0) we can
// transform it into a store of the expanded constant value.
if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) {
@@ -1456,7 +1455,7 @@
unsigned NumBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
if (NumBytes != 0) {
unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue();
-
+
// Compute the value replicated the right number of times.
APInt APVal(NumBytes*8, Val);
@@ -1464,7 +1463,7 @@
if (Val)
for (unsigned i = 1; i != NumBytes; ++i)
APVal |= APVal << 8;
-
+
// FIXME: Remove once builder has Twine API.
Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").str().c_str());
Value *New = ConvertScalar_InsertValue(
@@ -1480,19 +1479,19 @@
// can handle it like a load or store of the scalar type.
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) {
assert(Offset == 0 && "must be store to start of alloca");
-
+
// If the source and destination are both to the same alloca, then this is
// a noop copy-to-self, just delete it. Otherwise, emit a load and store
// as appropriate.
AllocaInst *OrigAI = cast<AllocaInst>(Ptr->getUnderlyingObject());
-
+
if (MTI->getSource()->getUnderlyingObject() != OrigAI) {
// Dest must be OrigAI, change this to be a load from the original
// pointer (bitcasted), then a store to our new alloca.
assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?");
Value *SrcPtr = MTI->getSource();
SrcPtr = Builder.CreateBitCast(SrcPtr, NewAI->getType());
-
+
LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval");
SrcVal->setAlignment(MTI->getAlignment());
Builder.CreateStore(SrcVal, NewAI);
@@ -1508,12 +1507,12 @@
} else {
// Noop transfer. Src == Dst
}
-
+
MTI->eraseFromParent();
continue;
}
-
+
// If user is a dbg info intrinsic then it is safe to remove it.
if (isa<DbgInfoIntrinsic>(User)) {
User->eraseFromParent();
@@ -1563,7 +1562,7 @@
V = Builder.CreateBitCast(V, ToType, "tmp");
return V;
}
-
+
// If ToType is a first class aggregate, extract out each of the pieces and
// use insertvalue's to form the FCA.
if (const StructType *ST = dyn_cast<StructType>(ToType)) {
@@ -1577,7 +1576,7 @@
}
return Res;
}
-
+
if (const ArrayType *AT = dyn_cast<ArrayType>(ToType)) {
uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType());
Value *Res = Context.getUndef(AT);
@@ -1613,7 +1612,7 @@
ConstantInt::get(FromVal->getType(),
ShAmt), "tmp");
else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth())
- FromVal = Builder.CreateShl(FromVal,
+ FromVal = Builder.CreateShl(FromVal,
ConstantInt::get(FromVal->getType(),
-ShAmt), "tmp");
@@ -1661,7 +1660,7 @@
if (const VectorType *VTy = dyn_cast<VectorType>(AllocaType)) {
uint64_t VecSize = TD->getTypeAllocSizeInBits(VTy);
uint64_t ValSize = TD->getTypeAllocSizeInBits(SV->getType());
-
+
// Changing the whole vector with memset or with an access of a different
// vector type?
if (ValSize == VecSize)
@@ -1671,28 +1670,28 @@
// Must be an element insertion.
unsigned Elt = Offset/EltSize;
-
+
if (SV->getType() != VTy->getElementType())
SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp");
-
- SV = Builder.CreateInsertElement(Old, SV,
+
+ SV = Builder.CreateInsertElement(Old, SV,
ConstantInt::get(Type::Int32Ty, Elt),
"tmp");
return SV;
}
-
+
// If SV is a first-class aggregate value, insert each value recursively.
if (const StructType *ST = dyn_cast<StructType>(SV->getType())) {
const StructLayout &Layout = *TD->getStructLayout(ST);
for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
Value *Elt = Builder.CreateExtractValue(SV, i, "tmp");
- Old = ConvertScalar_InsertValue(Elt, Old,
+ Old = ConvertScalar_InsertValue(Elt, Old,
Offset+Layout.getElementOffsetInBits(i),
Builder);
}
return Old;
}
-
+
if (const ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) {
uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType());
for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
@@ -1772,7 +1771,7 @@
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
return GV->isConstant();
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
- if (CE->getOpcode() == Instruction::BitCast ||
+ if (CE->getOpcode() == Instruction::BitCast ||
CE->getOpcode() == Instruction::GetElementPtr)
return PointsToConstantGlobal(CE->getOperand(0));
return false;
@@ -1792,7 +1791,7 @@
// Ignore non-volatile loads, they are always ok.
if (!LI->isVolatile())
continue;
-
+
if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) {
// If uses of the bitcast are ok, we are ok.
if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset))
@@ -1807,7 +1806,7 @@
return false;
continue;
}
-
+
// If this is isn't our memcpy/memmove, reject it as something we can't
// handle.
if (!isa<MemTransferInst>(*UI))
@@ -1815,20 +1814,20 @@
// If we already have seen a copy, reject the second one.
if (TheCopy) return false;
-
+
// If the pointer has been offset from the start of the alloca, we can't
// safely handle this.
if (isOffset) return false;
// If the memintrinsic isn't using the alloca as the dest, reject it.
if (UI.getOperandNo() != 1) return false;
-
+
MemIntrinsic *MI = cast<MemIntrinsic>(*UI);
-
+
// If the source of the memcpy/move is not a constant global, reject it.
if (!PointsToConstantGlobal(MI->getOperand(2)))
return false;
-
+
// Otherwise, the transform is safe. Remember the copy instruction.
TheCopy = MI;
}
Index: lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- lib/Transforms/IPO/GlobalOpt.cpp (revision 77596)
+++ lib/Transforms/IPO/GlobalOpt.cpp (working copy)
@@ -130,7 +130,7 @@
/// HasPHIUser - Set to true if this global has a user that is a PHI node.
bool HasPHIUser;
-
+
GlobalStatus() : isLoaded(false), StoredType(NotStored), StoredOnceValue(0),
AccessingFunction(0), HasMultipleAccessingFunctions(false),
HasNonInstructionUser(false), HasPHIUser(false) {}
@@ -306,7 +306,7 @@
if (Init)
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE, Context);
Changed |= CleanupConstantGlobalUsers(CE, SubInit, Context);
- } else if (CE->getOpcode() == Instruction::BitCast &&
+ } else if (CE->getOpcode() == Instruction::BitCast &&
isa<PointerType>(CE->getType())) {
// Pointer cast, delete any stores and memsets to the global.
Changed |= CleanupConstantGlobalUsers(CE, 0, Context);
@@ -322,7 +322,7 @@
// and will invalidate our notion of what Init is.
Constant *SubInit = 0;
if (!isa<ConstantExpr>(GEP->getOperand(0))) {
- ConstantExpr *CE =
+ ConstantExpr *CE =
dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, Context));
if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE, Context);
@@ -359,7 +359,7 @@
// We might have a dead and dangling constant hanging off of here.
if (Constant *C = dyn_cast<Constant>(V))
return SafeToDestroyConstant(C);
-
+
Instruction *I = dyn_cast<Instruction>(V);
if (!I) return false;
@@ -369,15 +369,15 @@
// Stores *to* the pointer are ok.
if (StoreInst *SI = dyn_cast<StoreInst>(I))
return SI->getOperand(0) != V;
-
+
// Otherwise, it must be a GEP.
GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
if (GEPI == 0) return false;
-
+
if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
!cast<Constant>(GEPI->getOperand(1))->isNullValue())
return false;
-
+
for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end();
I != E; ++I)
if (!isSafeSROAElementUse(*I))
@@ -391,11 +391,11 @@
///
static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
// The user of the global must be a GEP Inst or a ConstantExpr GEP.
- if (!isa<GetElementPtrInst>(U) &&
- (!isa<ConstantExpr>(U) ||
+ if (!isa<GetElementPtrInst>(U) &&
+ (!isa<ConstantExpr>(U) ||
cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
return false;
-
+
// 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
@@ -407,18 +407,18 @@
gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
++GEPI; // Skip over the pointer index.
-
+
// If this is a use of an array allocation, do a bit more checking for sanity.
if (const ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) {
uint64_t NumElements = AT->getNumElements();
ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
-
+
// 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;
-
+
// 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
@@ -434,7 +434,7 @@
NumElements = SubArrayTy->getNumElements();
else
NumElements = cast<VectorType>(*GEPI)->getNumElements();
-
+
ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
return false;
@@ -458,8 +458,8 @@
}
return true;
}
-
+
/// SRAGlobal - 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
@@ -470,7 +470,7 @@
// Make sure this global only has simple uses that we can SRA.
if (!GlobalUsersSafeToSRA(GV))
return 0;
-
+
assert(GV->hasLocalLinkage() && !GV->isConstant());
Constant *Init = GV->getInitializer();
const Type *Ty = Init->getType();
@@ -482,11 +482,11 @@
unsigned StartAlignment = GV->getAlignment();
if (StartAlignment == 0)
StartAlignment = TD.getABITypeAlignment(GV->getType());
-
+
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
NewGlobals.reserve(STy->getNumElements());
const StructLayout &Layout = *TD.getStructLayout(STy);
- for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+ for (uint32_t i = 0, e = STy->getNumElements(); i != e; ++i) {
Constant *In = getAggregateConstantElement(Init,
ConstantInt::get(Type::Int32Ty, i),
Context);
@@ -494,12 +494,12 @@
GlobalVariable *NGV = new GlobalVariable(Context,
STy->getElementType(i), false,
GlobalVariable::InternalLinkage,
- In, GV->getName()+"."+i,
+ In, GV->getName()+"."+ Twine(i),
GV->isThreadLocal(),
GV->getType()->getAddressSpace());
Globals.insert(GV, NGV);
NewGlobals.push_back(NGV);
-
+
// 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.
@@ -518,10 +518,10 @@
if (NumElements > 16 && GV->hasNUsesOrMore(16))
return 0; // It's not worth it.
NewGlobals.reserve(NumElements);
-
+
uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType());
unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
- for (unsigned i = 0, e = NumElements; i != e; ++i) {
+ for (uint32_t i = 0, e = NumElements; i != e; ++i) {
Constant *In = getAggregateConstantElement(Init,
ConstantInt::get(Type::Int32Ty, i),
Context);
@@ -530,12 +530,12 @@
GlobalVariable *NGV = new GlobalVariable(Context,
STy->getElementType(), false,
GlobalVariable::InternalLinkage,
- In, GV->getName()+"."+i,
+ In, GV->getName()+"."+ Twine(i),
GV->isThreadLocal(),
GV->getType()->getAddressSpace());
Globals.insert(GV, NGV);
NewGlobals.push_back(NGV);
-
+
// 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.
@@ -584,7 +584,8 @@
for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
Idxs.push_back(GEPI->getOperand(i));
NewPtr = GetElementPtrInst::Create(NewPtr, Idxs.begin(), Idxs.end(),
- GEPI->getName()+"."+Val, GEPI);
+ GEPI->getName()+"."+ Twine((uint32_t)Val),
+ GEPI);
}
}
GEP->replaceAllUsesWith(NewPtr);
@@ -613,7 +614,7 @@
}
/// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
-/// value will trap if the value is dynamically null. PHIs keeps track of any
+/// value will trap if the value is dynamically null. PHIs keeps track of any
/// phi nodes we've seen to avoid reprocessing them.
static bool AllUsesOfValueWillTrapIfNull(Value *V,
SmallPtrSet<PHINode*, 8> &PHIs) {
@@ -749,7 +750,7 @@
// 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;
-
+
// Replace all uses of loads with uses of uses of the stored value.
for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){
User *GlobalUser = *GUI++;
@@ -845,19 +846,19 @@
// could depend on malloc returning large alignment (on the mac, 16 bytes) but
// this would only guarantee some lower alignment.
Constant *Init = Context.getUndef(MI->getAllocatedType());
- GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
+ GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
MI->getAllocatedType(), false,
GlobalValue::InternalLinkage, Init,
GV->getName()+".body",
GV,
GV->isThreadLocal());
-
+
// Anything that used the malloc now uses the global directly.
MI->replaceAllUsesWith(NewGV);
Constant *RepValue = NewGV;
if (NewGV->getType() != GV->getType()->getElementType())
- RepValue = ConstantExpr::getBitCast(RepValue,
+ RepValue = ConstantExpr::getBitCast(RepValue,
GV->getType()->getElementType());
// If there is a comparison against null, we will insert a global bool to
@@ -944,23 +945,23 @@
SmallPtrSet<PHINode*, 8> &PHIs) {
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
Instruction *Inst = cast<Instruction>(*UI);
-
+
if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
continue; // Fine, ignore.
}
-
+
if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
return false; // Storing the pointer itself... bad.
continue; // Otherwise, storing through it, or storing into GV... fine.
}
-
+
if (isa<GetElementPtrInst>(Inst)) {
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
return false;
continue;
}
-
+
if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
// PHIs are ok if all uses are ok. Don't infinitely recurse through PHI
// cycles.
@@ -969,13 +970,13 @@
return false;
continue;
}
-
+
if (BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
return false;
continue;
}
-
+
return false;
}
return true;
@@ -984,9 +985,9 @@
/// ReplaceUsesOfMallocWithGlobal - 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
+/// GV. This assumes that these value pass the
/// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
-static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
+static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
GlobalVariable *GV) {
while (!Alloc->use_empty()) {
Instruction *U = cast<Instruction>(*Alloc->use_begin());
@@ -1019,7 +1020,7 @@
continue;
}
}
-
+
// Insert a load from the global, and use it instead of the malloc.
Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
U->replaceUsesOfWith(Alloc, NL);
@@ -1036,24 +1037,24 @@
// pointer, and a getelementptr of a specific form.
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
Instruction *User = cast<Instruction>(*UI);
-
+
// Comparison against null is ok.
if (ICmpInst *ICI = dyn_cast<ICmpInst>(User)) {
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
return false;
continue;
}
-
+
// getelementptr is also ok, but only a simple form.
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
// Must index into the array and into the struct.
if (GEPI->getNumOperands() < 3)
return false;
-
+
// Otherwise the GEP is ok.
continue;
}
-
+
if (PHINode *PN = dyn_cast<PHINode>(User)) {
if (!LoadUsingPHIsPerLoad.insert(PN))
// This means some phi nodes are dependent on each other.
@@ -1062,19 +1063,19 @@
if (!LoadUsingPHIs.insert(PN))
// If we have already analyzed this PHI, then it is safe.
continue;
-
+
// Make sure all uses of the PHI are simple enough to transform.
if (!LoadUsesSimpleEnoughForHeapSRA(PN,
LoadUsingPHIs, LoadUsingPHIsPerLoad))
return false;
-
+
continue;
}
-
+
// Otherwise we don't know what this is, not ok.
return false;
}
-
+
return true;
}
@@ -1085,7 +1086,7 @@
MallocInst *MI) {
SmallPtrSet<PHINode*, 32> LoadUsingPHIs;
SmallPtrSet<PHINode*, 32> LoadUsingPHIsPerLoad;
- for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;
+ for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;
++UI)
if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
@@ -1093,10 +1094,10 @@
return false;
LoadUsingPHIsPerLoad.clear();
}
-
+
// 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
- // that all inputs the to the PHI nodes are in the same equivalence sets.
+ // that all inputs the to the PHI nodes are in the same equivalence sets.
// Check to verify that all operands of the PHIs are either PHIS that can be
// transformed, loads from GV, or MI itself.
for (SmallPtrSet<PHINode*, 32>::iterator I = LoadUsingPHIs.begin(),
@@ -1104,29 +1105,29 @@
PHINode *PN = *I;
for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
Value *InVal = PN->getIncomingValue(op);
-
+
// PHI of the stored value itself is ok.
if (InVal == MI) continue;
-
+
if (PHINode *InPN = dyn_cast<PHINode>(InVal)) {
// One of the PHIs in our set is (optimistically) ok.
if (LoadUsingPHIs.count(InPN))
continue;
return false;
}
-
+
// Load from GV is ok.
if (LoadInst *LI = dyn_cast<LoadInst>(InVal))
if (LI->getOperand(0) == GV)
continue;
-
+
// UNDEF? NULL?
-
+
// Anything else is rejected.
return false;
}
}
-
+
return true;
}
@@ -1135,15 +1136,15 @@
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite,
LLVMContext &Context) {
std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
-
+
if (FieldNo >= FieldVals.size())
FieldVals.resize(FieldNo+1);
-
+
// If we already have this value, just reuse the previously scalarized
// version.
if (Value *FieldVal = FieldVals[FieldNo])
return FieldVal;
-
+
// Depending on what instruction this is, we have several cases.
Value *Result;
if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
@@ -1152,28 +1153,28 @@
Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
InsertedScalarizedValues,
PHIsToRewrite, Context),
- LI->getName()+".f" + FieldNo, LI);
+ LI->getName()+".f" + Twine((uint32_t)FieldNo), LI);
} else if (PHINode *PN = dyn_cast<PHINode>(V)) {
// PN's type is pointer to struct. Make a new PHI of pointer to struct
// field.
- const StructType *ST =
+ const StructType *ST =
cast<StructType>(cast<PointerType>(PN->getType())->getElementType());
-
+
Result =
PHINode::Create(PointerType::getUnqual(ST->getElementType(FieldNo)),
- PN->getName()+".f"+FieldNo, PN);
+ PN->getName()+".f"+Twine((uint32_t)FieldNo), PN);
PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
} else {
llvm_unreachable("Unknown usable value");
Result = 0;
}
-
+
return FieldVals[FieldNo] = Result;
}
/// RewriteHeapSROALoadUser - Given a load instruction and a value derived from
/// the load, rewrite the derived value to use the HeapSRoA'd load.
-static void RewriteHeapSROALoadUser(Instruction *LoadUser,
+static void RewriteHeapSROALoadUser(Instruction *LoadUser,
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite,
LLVMContext &Context) {
@@ -1185,31 +1186,31 @@
Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
InsertedScalarizedValues, PHIsToRewrite,
Context);
-
+
Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
- Context.getNullValue(NPtr->getType()),
+ Context.getNullValue(NPtr->getType()),
SCI->getName());
SCI->replaceAllUsesWith(New);
SCI->eraseFromParent();
return;
}
-
+
// Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
&& "Unexpected GEPI!");
-
+
// Load the pointer for this field.
unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
InsertedScalarizedValues, PHIsToRewrite,
Context);
-
+
// 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());
-
+
Value *NGEPI = GetElementPtrInst::Create(NewPtr,
GEPIdx.begin(), GEPIdx.end(),
GEPI->getName(), GEPI);
@@ -1230,7 +1231,7 @@
tie(InsertPos, Inserted) =
InsertedScalarizedValues.insert(std::make_pair(PN, std::vector<Value*>()));
if (!Inserted) return;
-
+
// If this is the first time we've seen this PHI, recursively process all
// users.
for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) {
@@ -1244,7 +1245,7 @@
/// is a value loaded from the global. Eliminate all uses of Ptr, making them
/// use FieldGlobals instead. All uses of loaded values satisfy
/// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
-static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
+static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite,
LLVMContext &Context) {
@@ -1254,7 +1255,7 @@
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite,
Context);
}
-
+
if (Load->use_empty()) {
Load->eraseFromParent();
InsertedScalarizedValues.erase(Load);
@@ -1273,30 +1274,30 @@
// the global to simplify later code. This also deletes the store
// into GV.
ReplaceUsesOfMallocWithGlobal(MI, GV);
-
+
// Okay, at this point, there are no users of the malloc. Insert N
// new mallocs at the same place as MI, and N globals.
std::vector<Value*> FieldGlobals;
std::vector<MallocInst*> FieldMallocs;
-
- for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
+
+ for (uint32_t FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
const Type *FieldTy = STy->getElementType(FieldNo);
const Type *PFieldTy = PointerType::getUnqual(FieldTy);
-
+
GlobalVariable *NGV =
new GlobalVariable(*GV->getParent(),
PFieldTy, false, GlobalValue::InternalLinkage,
Context.getNullValue(PFieldTy),
- GV->getName() + ".f" + FieldNo, GV,
+ GV->getName() + ".f" + Twine(FieldNo), GV,
GV->isThreadLocal());
FieldGlobals.push_back(NGV);
-
+
MallocInst *NMI = new MallocInst(FieldTy, MI->getArraySize(),
- MI->getName() + ".f" + FieldNo,MI);
+ MI->getName() + ".f" + Twine(FieldNo),MI);
FieldMallocs.push_back(NMI);
new StoreInst(NMI, NGV, MI);
}
-
+
// 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
@@ -1323,22 +1324,22 @@
// Split the basic block at the old malloc.
BasicBlock *OrigBB = MI->getParent();
BasicBlock *ContBB = OrigBB->splitBasicBlock(MI, "malloc_cont");
-
+
// 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.
BasicBlock *NullPtrBlock = BasicBlock::Create("malloc_ret_null",
OrigBB->getParent());
-
+
// 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);
-
+
// 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);
- Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
+ Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
Context.getNullValue(GVVal->getType()),
"tmp");
BasicBlock *FreeBlock = BasicBlock::Create("free_it", OrigBB->getParent());
@@ -1350,12 +1351,12 @@
new StoreInst(Context.getNullValue(GVVal->getType()), FieldGlobals[i],
FreeBlock);
BranchInst::Create(NextBlock, FreeBlock);
-
+
NullPtrBlock = NextBlock;
}
-
+
BranchInst::Create(ContBB, NullPtrBlock);
-
+
// MI is no longer needed, remove it.
MI->eraseFromParent();
@@ -1364,26 +1365,26 @@
/// inserted for a given load.
DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
InsertedScalarizedValues[GV] = FieldGlobals;
-
+
std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
-
+
// 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.
for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) {
Instruction *User = cast<Instruction>(*UI++);
-
+
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite,
Context);
continue;
}
-
+
// Must be a store of null.
StoreInst *SI = cast<StoreInst>(User);
assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
"Unexpected heap-sra user!");
-
+
// Insert a store of null into each global.
for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
const PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType());
@@ -1410,7 +1411,7 @@
FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
}
}
-
+
// 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();
@@ -1420,7 +1421,7 @@
else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
LI->dropAllReferences();
}
-
+
// 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();
@@ -1430,7 +1431,7 @@
else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
LI->eraseFromParent();
}
-
+
// The old global is now dead, remove it.
GV->eraseFromParent();
@@ -1449,7 +1450,7 @@
// If this is a malloc of an abstract type, don't touch it.
if (!MI->getAllocatedType()->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
@@ -1458,7 +1459,7 @@
// 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
// malloc to be stored into the specified global, loaded setcc'd, and
@@ -1469,8 +1470,8 @@
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV, PHIs))
return false;
}
-
-
+
+
// 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
@@ -1485,29 +1486,29 @@
return true;
}
}
-
+
// 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.
const Type *AllocTy = MI->getAllocatedType();
-
+
// 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
if (!MI->isArrayAllocation())
if (const ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
AllocTy = AT->getElementType();
-
+
if (const StructType *AllocSTy = dyn_cast<StructType>(AllocTy)) {
// This the structure has an unreasonable number of fields, leave it
// alone.
if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, MI)) {
-
+
// If this is a fixed size array, transform the Malloc to be an alloc of
// structs. malloc [100 x struct],1 -> malloc struct, 100
if (const ArrayType *AT = dyn_cast<ArrayType>(MI->getAllocatedType())) {
- MallocInst *NewMI =
- new MallocInst(AllocSTy,
+ MallocInst *NewMI =
+ new MallocInst(AllocSTy,
ConstantInt::get(Type::Int32Ty, AT->getNumElements()),
"", MI);
NewMI->takeName(MI);
@@ -1516,14 +1517,14 @@
MI->eraseFromParent();
MI = NewMI;
}
-
+
GVI = PerformHeapAllocSRoA(GV, MI, Context);
return true;
}
}
-
+
return false;
-}
+}
// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
// that only one value (besides its initializer) is ever stored to the global.
@@ -1541,7 +1542,7 @@
GV->getInitializer()->isNullValue()) {
if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
if (GV->getInitializer()->getType() != SOVC->getType())
- SOVC =
+ SOVC =
ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
// Optimize away any trapping uses of the loaded value.
@@ -1563,7 +1564,7 @@
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal,
LLVMContext &Context) {
const Type *GVElType = GV->getType()->getElementType();
-
+
// 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
@@ -1572,15 +1573,15 @@
if (GVElType == Type::Int1Ty || GVElType->isFloatingPoint() ||
isa<PointerType>(GVElType) || isa<VectorType>(GVElType))
return false;
-
+
// Walk the use list of the global seeing if all the uses are load or store.
// If there is anything else, bail out.
for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I)
if (!isa<LoadInst>(I) && !isa<StoreInst>(I))
return false;
-
+
DOUT << " *** SHRINKING TO BOOL: " << *GV;
-
+
// Create the new global, initializing it to false.
GlobalVariable *NewGV = new GlobalVariable(Context, Type::Int1Ty, false,
GlobalValue::InternalLinkage, Context.getFalse(),
@@ -1683,7 +1684,7 @@
cerr << " HasNonInstructionUser = " << GS.HasNonInstructionUser<<"\n";
cerr << "\n";
#endif
-
+
// If this is a first class global and has only one accessing function
// and this function is main (which we know is not recursive we can make
// this global a local variable) we replace the global with a local alloca
@@ -1712,7 +1713,7 @@
++NumLocalized;
return true;
}
-
+
// If the global is never loaded (but may be stored to), it is dead.
// Delete it now.
if (!GS.isLoaded) {
@@ -1720,7 +1721,7 @@
// Delete any stores we can find to the global. We may not be able to
// make it completely dead though.
- bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(),
+ bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(),
GV->getContext());
// If the global is dead now, delete it.
@@ -1749,7 +1750,7 @@
++NumMarked;
return true;
} else if (!GV->getInitializer()->getType()->isSingleValueType()) {
- if (GlobalVariable *FirstNewGV = SRAGlobal(GV,
+ if (GlobalVariable *FirstNewGV = SRAGlobal(GV,
getAnalysis<TargetData>(),
GV->getContext())) {
GVI = FirstNewGV; // Don't skip the newly produced globals!
@@ -1766,7 +1767,7 @@
GV->setInitializer(SOVConstant);
// Clean up any obviously simplifiable users now.
- CleanupConstantGlobalUsers(GV, GV->getInitializer(),
+ CleanupConstantGlobalUsers(GV, GV->getInitializer(),
GV->getContext());
if (GV->use_empty()) {
@@ -1900,7 +1901,7 @@
if (!FTy || FTy->getReturnType() != Type::VoidTy || FTy->isVarArg() ||
FTy->getNumParams() != 0)
return 0;
-
+
// Verify that the initializer is simple enough for us to handle.
if (!I->hasInitializer()) return 0;
ConstantArray *CA = dyn_cast<ConstantArray>(I->getInitializer());
@@ -1913,7 +1914,7 @@
// Must have a function or null ptr.
if (!isa<Function>(CS->getOperand(1)))
return 0;
-
+
// Init priority must be standard.
ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
if (!CI || CI->getZExtValue() != 65535)
@@ -1921,7 +1922,7 @@
} else {
return 0;
}
-
+
return I;
}
return 0;
@@ -1942,14 +1943,14 @@
/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
/// specified array, returning the new global to use.
-static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
+static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
const std::vector<Function*> &Ctors,
LLVMContext &Context) {
// If we made a change, reassemble the initializer list.
std::vector<Constant*> CSVals;
CSVals.push_back(ConstantInt::get(Type::Int32Ty, 65535));
CSVals.push_back(0);
-
+
// Create the new init list.
std::vector<Constant*> CAList;
for (unsigned i = 0, e = Ctors.size(); i != e; ++i) {
@@ -1963,27 +1964,27 @@
}
CAList.push_back(ConstantStruct::get(CSVals));
}
-
+
// Create the array initializer.
const Type *StructTy =
cast<ArrayType>(GCL->getType()->getElementType())->getElementType();
- Constant *CA = ConstantArray::get(ArrayType::get(StructTy,
+ Constant *CA = ConstantArray::get(ArrayType::get(StructTy,
CAList.size()), CAList);
-
+
// If we didn't change the number of elements, don't create a new GV.
if (CA->getType() == GCL->getInitializer()->getType()) {
GCL->setInitializer(CA);
return GCL;
}
-
+
// Create the new global and insert it next to the existing list.
- GlobalVariable *NGV = new GlobalVariable(Context, CA->getType(),
+ GlobalVariable *NGV = new GlobalVariable(Context, CA->getType(),
GCL->isConstant(),
GCL->getLinkage(), CA, "",
GCL->isThreadLocal());
GCL->getParent()->getGlobalList().insert(GCL, NGV);
NGV->takeName(GCL);
-
+
// Nuke the old list, replacing any uses with the new one.
if (!GCL->use_empty()) {
Constant *V = NGV;
@@ -1992,7 +1993,7 @@
GCL->replaceAllUsesWith(V);
}
GCL->eraseFromParent();
-
+
if (Ctors.size())
return NGV;
else
@@ -2043,7 +2044,7 @@
assert(Val->getType() == Init->getType() && "Type mismatch!");
return Val;
}
-
+
if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
std::vector<Constant*> Elts;
@@ -2061,13 +2062,13 @@
llvm_unreachable("This code is out of sync with "
" ConstantFoldLoadThroughGEPConstantExpr");
}
-
+
// 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, Context);
-
+
// Return the modified struct.
return ConstantStruct::get(&Elts[0], Elts.size(), STy->isPacked());
} else {
@@ -2089,12 +2090,12 @@
llvm_unreachable("This code is out of sync with "
" ConstantFoldLoadThroughGEPConstantExpr");
}
-
+
assert(CI->getZExtValue() < ATy->getNumElements());
Elts[CI->getZExtValue()] =
EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1, Context);
return ConstantArray::get(ATy, Elts);
- }
+ }
}
/// CommitValueTo - We have decided that Addr (which satisfies the predicate
@@ -2106,10 +2107,10 @@
GV->setInitializer(Val);
return;
}
-
+
ConstantExpr *CE = cast<ConstantExpr>(Addr);
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
-
+
Constant *Init = GV->getInitializer();
Init = EvaluateStoreInto(Init, Val, CE, 2, Context);
GV->setInitializer(Init);
@@ -2125,14 +2126,14 @@
// is the most up-to-date.
DenseMap<Constant*, Constant*>::const_iterator I = Memory.find(P);
if (I != Memory.end()) return I->second;
-
+
// Access it.
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
if (GV->hasInitializer())
return GV->getInitializer();
return 0;
}
-
+
// Handle a constantexpr getelementptr.
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
if (CE->getOpcode() == Instruction::GetElementPtr &&
@@ -2158,14 +2159,14 @@
// bail out. TODO: we might want to accept limited recursion.
if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
return false;
-
+
LLVMContext &Context = F->getContext();
-
+
CallStack.push_back(F);
-
+
/// Values - As we compute SSA register values, we store their contents here.
DenseMap<Value*, Constant*> Values;
-
+
// Initialize arguments to the incoming values specified.
unsigned ArgNo = 0;
for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
@@ -2176,14 +2177,14 @@
/// 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;
-
+
// CurInst - The current instruction we're evaluating.
BasicBlock::iterator CurInst = F->begin()->begin();
-
+
// This is the main evaluation loop.
while (1) {
Constant *InstResult = 0;
-
+
if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
if (SI->isVolatile()) return false; // no volatile accesses.
Constant *Ptr = getVal(Values, SI->getOperand(1));
@@ -2229,7 +2230,7 @@
GlobalValue::InternalLinkage,
Context.getUndef(Ty),
AI->getName()));
- InstResult = AllocaTmps.back();
+ InstResult = AllocaTmps.back();
} else if (CallInst *CI = dyn_cast<CallInst>(CurInst)) {
// Debug info can safely be ignored here.
@@ -2249,7 +2250,7 @@
for (User::op_iterator i = CI->op_begin() + 1, e = CI->op_end();
i != e; ++i)
Formals.push_back(getVal(Values, *i));
-
+
if (Callee->isDeclaration()) {
// If this is a function we can constant fold, do it.
if (Constant *C = ConstantFoldCall(Callee, &Formals[0],
@@ -2261,7 +2262,7 @@
} else {
if (Callee->getFunctionType()->isVarArg())
return false;
-
+
Constant *RetVal;
// Execute the call, if successful, use the return value.
if (!EvaluateFunction(Callee, RetVal, Formals, CallStack,
@@ -2279,7 +2280,7 @@
dyn_cast<ConstantInt>(getVal(Values, BI->getCondition()));
if (!Cond) return false; // Cannot determine.
- NewBB = BI->getSuccessor(!Cond->getZExtValue());
+ NewBB = BI->getSuccessor(!Cond->getZExtValue());
}
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
ConstantInt *Val =
@@ -2289,20 +2290,20 @@
} else if (ReturnInst *RI = dyn_cast<ReturnInst>(CurInst)) {
if (RI->getNumOperands())
RetVal = getVal(Values, RI->getOperand(0));
-
+
CallStack.pop_back(); // return from fn.
return true; // We succeeded at evaluating this ctor!
} else {
// invoke, unwind, unreachable.
return false; // Cannot handle this terminator.
}
-
+
// 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.
if (!ExecutedBlocks.insert(NewBB))
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.
@@ -2318,10 +2319,10 @@
// Did not know how to evaluate this!
return false;
}
-
+
if (!CurInst->use_empty())
Values[CurInst] = InstResult;
-
+
// Advance program counter.
++CurInst;
}
@@ -2339,7 +2340,7 @@
/// to represent its body. This vector is needed so we can delete the
/// temporary globals when we are done.
std::vector<GlobalVariable*> AllocaTmps;
-
+
/// CallStack - This is used to detect recursion. In pathological situations
/// we could hit exponential behavior, but at least there is nothing
/// unbounded.
@@ -2358,13 +2359,13 @@
E = MutatedMemory.end(); I != E; ++I)
CommitValueTo(I->second, I->first, F->getContext());
}
-
+
// At this point, we are done interpreting. If we created any 'alloca'
// temporaries, release them now.
while (!AllocaTmps.empty()) {
GlobalVariable *Tmp = AllocaTmps.back();
AllocaTmps.pop_back();
-
+
// 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.
@@ -2372,7 +2373,7 @@
Tmp->replaceAllUsesWith(F->getContext().getNullValue(Tmp->getType()));
delete Tmp;
}
-
+
return EvalSuccess;
}
@@ -2384,7 +2385,7 @@
std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
bool MadeChange = false;
if (Ctors.empty()) return false;
-
+
// Loop over global ctors, optimizing them when we can.
for (unsigned i = 0; i != Ctors.size(); ++i) {
Function *F = Ctors[i];
@@ -2397,10 +2398,10 @@
}
break;
}
-
+
// We cannot simplify external ctor functions.
if (F->empty()) continue;
-
+
// If we can evaluate the ctor at compile time, do.
if (EvaluateStaticConstructor(F)) {
Ctors.erase(Ctors.begin()+i);
@@ -2410,9 +2411,9 @@
continue;
}
}
-
+
if (!MadeChange) return false;
-
+
GCL = InstallGlobalCtors(GCL, Ctors, GCL->getContext());
return true;
}
@@ -2477,21 +2478,21 @@
bool GlobalOpt::runOnModule(Module &M) {
bool Changed = false;
-
+
// Try to find the llvm.globalctors list.
GlobalVariable *GlobalCtors = FindGlobalCtors(M);
bool LocalChange = true;
while (LocalChange) {
LocalChange = false;
-
+
// Delete functions that are trivially dead, ccc -> fastcc
LocalChange |= OptimizeFunctions(M);
-
+
// Optimize global_ctors list.
if (GlobalCtors)
LocalChange |= OptimizeGlobalCtorsList(GlobalCtors);
-
+
// Optimize non-address-taken globals.
LocalChange |= OptimizeGlobalVars(M);
@@ -2499,9 +2500,9 @@
LocalChange |= OptimizeGlobalAliases(M);
Changed |= LocalChange;
}
-
+
// TODO: Move all global ctors functions to the end of the module for code
// layout.
-
+
return Changed;
}
Index: lib/Transforms/IPO/ArgumentPromotion.cpp
===================================================================
--- lib/Transforms/IPO/ArgumentPromotion.cpp (revision 77596)
+++ lib/Transforms/IPO/ArgumentPromotion.cpp (working copy)
@@ -760,13 +760,13 @@
const StructType *STy = cast<StructType>(AgTy);
Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
- for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+ for (uint32_t i = 0, e = STy->getNumElements(); i != e; ++i) {
Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
- Value *Idx =
+ Value *Idx =
GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
- TheAlloca->getName()+"."+i,
+ TheAlloca->getName()+"."+ Twine(i),
InsertPt);
- I2->setName(I->getName()+"."+i);
+ I2->setName(I->getName()+"."+ Twine(i));
new StoreInst(I2++, Idx, InsertPt);
}
More information about the llvm-commits
mailing list