|
14 | 14 | #define DEBUG_TYPE "loop-rotate"
|
15 | 15 | #include "llvm/Transforms/Scalar.h"
|
16 | 16 | #include "llvm/Function.h"
|
17 |
| -#include "llvm/Analysis/LoopPass.h" |
18 |
| -#include "llvm/Analysis/DominanceFrontier.h" |
19 | 17 | #include "llvm/Analysis/CodeMetrics.h"
|
| 18 | +#include "llvm/Analysis/DominanceFrontier.h" |
| 19 | +#include "llvm/Analysis/LoopPass.h" |
| 20 | +#include "llvm/Analysis/InstructionSimplify.h" |
20 | 21 | #include "llvm/Analysis/ScalarEvolution.h"
|
21 | 22 | #include "llvm/Transforms/Utils/Local.h"
|
22 | 23 | #include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
@@ -205,9 +206,24 @@ bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
|
205 | 206 | // Otherwise, create a duplicate of the instruction.
|
206 | 207 | Instruction *C = Inst->clone();
|
207 | 208 |
|
208 |
| - C->setName(Inst->getName()); |
209 |
| - C->insertBefore(LoopEntryBranch); |
210 |
| - ValueMap[Inst] = C; |
| 209 | + // Eagerly remap the operands of the instruction. |
| 210 | + RemapInstruction(C, ValueMap, |
| 211 | + RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); |
| 212 | + |
| 213 | + // With the operands remapped, see if the instruction constant folds or is |
| 214 | + // otherwise simplifyable. This commonly occurs because the entry from PHI |
| 215 | + // nodes allows icmps and other instructions to fold. |
| 216 | + if (Value *V = SimplifyInstruction(C)) { |
| 217 | + // If so, then delete the temporary instruction and stick the folded value |
| 218 | + // in the map. |
| 219 | + delete C; |
| 220 | + ValueMap[Inst] = V; |
| 221 | + } else { |
| 222 | + // Otherwise, stick the new instruction into the new block! |
| 223 | + C->setName(Inst->getName()); |
| 224 | + C->insertBefore(LoopEntryBranch); |
| 225 | + ValueMap[Inst] = C; |
| 226 | + } |
211 | 227 | }
|
212 | 228 |
|
213 | 229 | // Along with all the other instructions, we just cloned OrigHeader's
|
|
0 commit comments