Skip to content

Commit 96a4097

Browse files
committed
IROutliner: Use ValueMapper to remap constants in a function
Stop relying on the uselist of constants, which also required filtering the uses that are in the correct function. I'm not sure why this pass is doing its own cloning instead of building the value map while doing its cloning.
1 parent 69c4e17 commit 96a4097

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

llvm/lib/Transforms/IPO/IROutliner.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/PassManager.h"
2525
#include "llvm/Support/CommandLine.h"
2626
#include "llvm/Transforms/IPO.h"
27+
#include "llvm/Transforms/Utils/ValueMapper.h"
2728
#include <optional>
2829
#include <vector>
2930

@@ -1930,10 +1931,12 @@ replaceArgumentUses(OutlinableRegion &Region,
19301931
/// \param Region [in] - The region of extracted code to be changed.
19311932
void replaceConstants(OutlinableRegion &Region) {
19321933
OutlinableGroup &Group = *Region.Parent;
1934+
Function *OutlinedFunction = Group.OutlinedFunction;
1935+
ValueToValueMapTy VMap;
1936+
19331937
// Iterate over the constants that need to be elevated into arguments
19341938
for (std::pair<unsigned, Constant *> &Const : Region.AggArgToConstant) {
19351939
unsigned AggArgIdx = Const.first;
1936-
Function *OutlinedFunction = Group.OutlinedFunction;
19371940
assert(OutlinedFunction && "Overall Function is not defined?");
19381941
Constant *CST = Const.second;
19391942
Argument *Arg = Group.OutlinedFunction->getArg(AggArgIdx);
@@ -1943,16 +1946,14 @@ void replaceConstants(OutlinableRegion &Region) {
19431946
// TODO: If in the future constants do not have one global value number,
19441947
// i.e. a constant 1 could be mapped to several values, this check will
19451948
// have to be more strict. It cannot be using only replaceUsesWithIf.
1946-
1949+
VMap[CST] = Arg;
19471950
LLVM_DEBUG(dbgs() << "Replacing uses of constant " << *CST
19481951
<< " in function " << *OutlinedFunction << " with "
1949-
<< *Arg << "\n");
1950-
CST->replaceUsesWithIf(Arg, [OutlinedFunction](Use &U) {
1951-
if (Instruction *I = dyn_cast<Instruction>(U.getUser()))
1952-
return I->getFunction() == OutlinedFunction;
1953-
return false;
1954-
});
1952+
<< *Arg << '\n');
19551953
}
1954+
1955+
RemapFunction(*OutlinedFunction, VMap,
1956+
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
19561957
}
19571958

19581959
/// It is possible that there is a basic block that already performs the same

0 commit comments

Comments
 (0)