Skip to content

Commit 43f8d16

Browse files
committed
Revamp the ValueMapper interfaces in a couple ways:
1. Take a flags argument instead of a bool. This makes it more clear to the reader what it is used for. 2. Add a flag that says that "remapping a value not in the map is ok". 3. Reimplement MapValue to share a bunch of code and be a lot more efficient. For lookup failures, don't drop null values into the map. 4. Using the new flag a bunch of code can vaporize in LinkModules and LoopUnswitch, kill it. No functionality change. llvm-svn: 123058
1 parent 2b3f20e commit 43f8d16

File tree

7 files changed

+108
-166
lines changed

7 files changed

+108
-166
lines changed

llvm/include/llvm/Transforms/Utils/ValueMapper.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ namespace llvm {
2222
class Instruction;
2323
typedef ValueMap<const Value *, TrackingVH<Value> > ValueToValueMapTy;
2424

25+
/// RemapFlags - These are flags that the value mapping APIs allow.
26+
enum RemapFlags {
27+
RF_None = 0,
28+
29+
/// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that
30+
/// only local values within a function (such as an instruction or argument)
31+
/// are mapped, not global values like functions and global metadata.
32+
RF_NoModuleLevelChanges = 1,
33+
34+
/// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
35+
/// entries that are not in the value map. If it is unset, it aborts if an
36+
/// operand is asked to be remapped which doesn't exist in the mapping.
37+
RF_IgnoreMissingEntries = 2
38+
};
39+
40+
static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
41+
return RemapFlags(unsigned(LHS)|unsigned(RHS));
42+
}
43+
2544
Value *MapValue(const Value *V, ValueToValueMapTy &VM,
26-
bool ModuleLevelChanges);
45+
RemapFlags Flags = RF_None);
2746
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
28-
bool ModuleLevelChanges);
47+
RemapFlags Flags = RF_None);
2948
} // End llvm namespace
3049

3150
#endif

llvm/lib/Linker/LinkModules.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ static void LinkNamedMDNodes(Module *Dest, Module *Src,
451451
// Add Src elements into Dest node.
452452
for (unsigned i = 0, e = SrcNMD->getNumOperands(); i != e; ++i)
453453
DestNMD->addOperand(cast<MDNode>(MapValue(SrcNMD->getOperand(i),
454-
ValueMap,
455-
true)));
454+
ValueMap)));
456455
}
457456
}
458457

@@ -814,9 +813,9 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
814813
const GlobalVariable *SGV = I;
815814

816815
if (SGV->hasInitializer()) { // Only process initialized GV's
817-
// Figure out what the initializer looks like in the dest module...
816+
// Figure out what the initializer looks like in the dest module.
818817
Constant *SInit =
819-
cast<Constant>(MapValue(SGV->getInitializer(), ValueMap, true));
818+
cast<Constant>(MapValue(SGV->getInitializer(), ValueMap));
820819
// Grab destination global variable or alias.
821820
GlobalValue *DGV = cast<GlobalValue>(ValueMap[SGV]->stripPointerCasts());
822821

@@ -996,32 +995,10 @@ static bool LinkFunctionBody(Function *Dest, Function *Src,
996995
// At this point, all of the instructions and values of the function are now
997996
// copied over. The only problem is that they are still referencing values in
998997
// the Source function as operands. Loop through all of the operands of the
999-
// functions and patch them up to point to the local versions...
1000-
//
1001-
// This is the same as RemapInstruction, except that it avoids remapping
1002-
// instruction and basic block operands.
1003-
//
998+
// functions and patch them up to point to the local versions.
1004999
for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
1005-
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1006-
// Remap operands.
1007-
for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
1008-
OI != OE; ++OI)
1009-
if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI))
1010-
*OI = MapValue(*OI, ValueMap, true);
1011-
1012-
// Remap attached metadata.
1013-
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1014-
I->getAllMetadata(MDs);
1015-
for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
1016-
MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
1017-
Value *Old = MI->second;
1018-
if (!isa<Instruction>(Old) && !isa<BasicBlock>(Old)) {
1019-
Value *New = MapValue(Old, ValueMap, true);
1020-
if (New != Old)
1021-
I->setMetadata(MI->first, cast<MDNode>(New));
1022-
}
1023-
}
1024-
}
1000+
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1001+
RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries);
10251002

10261003
// There is no need to map the arguments anymore.
10271004
for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();

llvm/lib/Transforms/Scalar/LoopRotation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
2323
#include "llvm/Transforms/Utils/SSAUpdater.h"
2424
#include "llvm/Transforms/Utils/ValueMapper.h"
25-
#include "llvm/Support/CommandLine.h"
2625
#include "llvm/Support/Debug.h"
2726
#include "llvm/ADT/Statistic.h"
2827
using namespace llvm;
@@ -205,6 +204,7 @@ bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
205204

206205
// Otherwise, create a duplicate of the instruction.
207206
Instruction *C = Inst->clone();
207+
208208
C->setName(Inst->getName());
209209
C->insertBefore(LoopEntryBranch);
210210
ValueMap[Inst] = C;

llvm/lib/Transforms/Scalar/LoopUnswitch.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,6 @@ bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val) {
457457
return true;
458458
}
459459

460-
// RemapInstruction - Convert the instruction operands from referencing the
461-
// current values into those specified by VMap.
462-
//
463-
static inline void RemapInstruction(Instruction *I,
464-
ValueToValueMapTy &VMap) {
465-
for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
466-
Value *Op = I->getOperand(op);
467-
ValueToValueMapTy::iterator It = VMap.find(Op);
468-
if (It != VMap.end()) Op = It->second;
469-
I->setOperand(op, Op);
470-
}
471-
}
472-
473460
/// CloneLoop - Recursively clone the specified loop and all of its children,
474461
/// mapping the blocks with the specified map.
475462
static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
@@ -664,7 +651,7 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
664651
for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
665652
for (BasicBlock::iterator I = NewBlocks[i]->begin(),
666653
E = NewBlocks[i]->end(); I != E; ++I)
667-
RemapInstruction(I, VMap);
654+
RemapInstruction(I, VMap,RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
668655

669656
// Rewrite the original preheader to select between versions of the loop.
670657
BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator());

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
112112
const BasicBlock &BB = *BI;
113113

114114
// Create a new basic block and copy instructions into it!
115-
BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc,
116-
CodeInfo);
115+
BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
117116
VMap[&BB] = CBB; // Add basic block mapping.
118117

119118
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
@@ -122,12 +121,12 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
122121

123122
// Loop over all of the instructions in the function, fixing up operand
124123
// references as we go. This uses VMap to do all the hard work.
125-
//
126124
for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]),
127125
BE = NewFunc->end(); BB != BE; ++BB)
128126
// Loop over all instructions, fixing each one as we find it...
129127
for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
130-
RemapInstruction(II, VMap, ModuleLevelChanges);
128+
RemapInstruction(II, VMap,
129+
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
131130
}
132131

133132
/// CloneFunction - Return a copy of the specified function, but without
@@ -138,8 +137,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
138137
/// updated to include mappings from all of the instructions and basicblocks in
139138
/// the function from their old to new values.
140139
///
141-
Function *llvm::CloneFunction(const Function *F,
142-
ValueToValueMapTy &VMap,
140+
Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
143141
bool ModuleLevelChanges,
144142
ClonedCodeInfo *CodeInfo) {
145143
std::vector<const Type*> ArgTypes;
@@ -322,7 +320,8 @@ ConstantFoldMappedInstruction(const Instruction *I) {
322320
SmallVector<Constant*, 8> Ops;
323321
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
324322
if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i),
325-
VMap, ModuleLevelChanges)))
323+
VMap,
324+
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges)))
326325
Ops.push_back(Op);
327326
else
328327
return 0; // All operands not constant!
@@ -460,7 +459,8 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
460459
I->setDebugLoc(DebugLoc());
461460
}
462461
}
463-
RemapInstruction(I, VMap, ModuleLevelChanges);
462+
RemapInstruction(I, VMap,
463+
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
464464
}
465465
}
466466

@@ -480,10 +480,10 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
480480
PHINode *PN = cast<PHINode>(VMap[OPN]);
481481
for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
482482
Value *V = VMap[PN->getIncomingBlock(pred)];
483-
if (BasicBlock *MappedBlock =
484-
cast_or_null<BasicBlock>(V)) {
483+
if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
485484
Value *InVal = MapValue(PN->getIncomingValue(pred),
486-
VMap, ModuleLevelChanges);
485+
VMap,
486+
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
487487
assert(InVal && "Unknown input value?");
488488
PN->setIncomingValue(pred, InVal);
489489
PN->setIncomingBlock(pred, MappedBlock);

llvm/lib/Transforms/Utils/CloneModule.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ Module *llvm::CloneModule(const Module *M,
8989
GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
9090
if (I->hasInitializer())
9191
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
92-
VMap,
93-
true)));
92+
VMap, RF_None)));
9493
GV->setLinkage(I->getLinkage());
9594
GV->setThreadLocal(I->isThreadLocal());
9695
GV->setConstant(I->isConstant());
@@ -121,7 +120,7 @@ Module *llvm::CloneModule(const Module *M,
121120
GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
122121
GA->setLinkage(I->getLinkage());
123122
if (const Constant* C = I->getAliasee())
124-
GA->setAliasee(cast<Constant>(MapValue(C, VMap, true)));
123+
GA->setAliasee(cast<Constant>(MapValue(C, VMap, RF_None)));
125124
}
126125

127126
// And named metadata....
@@ -130,7 +129,8 @@ Module *llvm::CloneModule(const Module *M,
130129
const NamedMDNode &NMD = *I;
131130
NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
132131
for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
133-
NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap, true)));
132+
NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap,
133+
RF_None)));
134134
}
135135

136136
return New;

0 commit comments

Comments
 (0)