|
24 | 24 | #include "llvm/IR/PassManager.h"
|
25 | 25 | #include "llvm/Support/CommandLine.h"
|
26 | 26 | #include "llvm/Transforms/IPO.h"
|
| 27 | +#include "llvm/Transforms/Utils/ValueMapper.h" |
27 | 28 | #include <optional>
|
28 | 29 | #include <vector>
|
29 | 30 |
|
@@ -1930,29 +1931,25 @@ replaceArgumentUses(OutlinableRegion &Region,
|
1930 | 1931 | /// \param Region [in] - The region of extracted code to be changed.
|
1931 | 1932 | void replaceConstants(OutlinableRegion &Region) {
|
1932 | 1933 | OutlinableGroup &Group = *Region.Parent;
|
| 1934 | + Function *OutlinedFunction = Group.OutlinedFunction; |
| 1935 | + ValueToValueMapTy VMap; |
| 1936 | + |
1933 | 1937 | // Iterate over the constants that need to be elevated into arguments
|
1934 | 1938 | for (std::pair<unsigned, Constant *> &Const : Region.AggArgToConstant) {
|
1935 | 1939 | unsigned AggArgIdx = Const.first;
|
1936 |
| - Function *OutlinedFunction = Group.OutlinedFunction; |
1937 | 1940 | assert(OutlinedFunction && "Overall Function is not defined?");
|
1938 | 1941 | Constant *CST = Const.second;
|
1939 | 1942 | Argument *Arg = Group.OutlinedFunction->getArg(AggArgIdx);
|
1940 | 1943 | // Identify the argument it will be elevated to, and replace instances of
|
1941 | 1944 | // 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; |
1947 | 1946 | LLVM_DEBUG(dbgs() << "Replacing uses of constant " << *CST
|
1948 | 1947 | << " 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'); |
1955 | 1949 | }
|
| 1950 | + |
| 1951 | + RemapFunction(*OutlinedFunction, VMap, |
| 1952 | + RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); |
1956 | 1953 | }
|
1957 | 1954 |
|
1958 | 1955 | /// It is possible that there is a basic block that already performs the same
|
|
0 commit comments