Skip to content

Commit a80a802

Browse files
authored
IROutliner: Use ValueMapper to remap constants in a function (#134850)
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 edd7b55 commit a80a802

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

llvm/lib/Transforms/IPO/IROutliner.cpp

Lines changed: 9 additions & 12 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,29 +1931,25 @@ 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);
19401943
// Identify the argument it will be elevated to, and replace instances of
19411944
// that constant in the function.
1942-
1943-
// TODO: If in the future constants do not have one global value number,
1944-
// i.e. a constant 1 could be mapped to several values, this check will
1945-
// have to be more strict. It cannot be using only replaceUsesWithIf.
1946-
1945+
VMap[CST] = Arg;
19471946
LLVM_DEBUG(dbgs() << "Replacing uses of constant " << *CST
19481947
<< " 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-
});
1948+
<< *Arg << '\n');
19551949
}
1950+
1951+
RemapFunction(*OutlinedFunction, VMap,
1952+
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
19561953
}
19571954

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

0 commit comments

Comments
 (0)