llvm.org GIT mirror
Add a loop rerolling pass This adds a loop rerolling pass: the opposite of (partial) loop unrolling. The transformation aims to take loops like this: for (int i = 0; i < 3200; i += 5) { a[i] += alpha * b[i]; a[i + 1] += alpha * b[i + 1]; a[i + 2] += alpha * b[i + 2]; a[i + 3] += alpha * b[i + 3]; a[i + 4] += alpha * b[i + 4]; } and turn them into this: for (int i = 0; i < 3200; ++i) { a[i] += alpha * b[i]; } and loops like this: for (int i = 0; i < 500; ++i) { x[3*i] = foo(0); x[3*i+1] = foo(0); x[3*i+2] = foo(0); } and turn them into this: for (int i = 0; i < 1500; ++i) { x[i] = foo(0); } There are two motivations for this transformation: 1. Code-size reduction (especially relevant, obviously, when compiling for code size). 2. Providing greater choice to the loop vectorizer (and generic unroller) to choose the unrolling factor (and a better ability to vectorize). The loop vectorizer can take vector lengths and register pressure into account when choosing an unrolling factor, for example, and a pre-unrolled loop limits that choice. This is especially problematic if the manual unrolling was optimized for a machine different from the current target. The current implementation is limited to single basic-block loops only. The rerolling recognition should work regardless of how the loop iterations are intermixed within the loop body (subject to dependency and side-effect constraints), but the significant restriction is that the order of the instructions in each iteration must be identical. This seems sufficient to capture all current use cases. This pass is not currently enabled by default at any optimization level. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194939 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 5 years ago
10 changed file(s) with 1630 addition(s) and 0 deletion(s).
 161 161 void initializeLoopSimplifyPass(PassRegistry&); 162 162 void initializeLoopStrengthReducePass(PassRegistry&); 163 163 void initializeGlobalMergePass(PassRegistry&); 164 void initializeLoopRerollPass(PassRegistry&); 164 165 void initializeLoopUnrollPass(PassRegistry&); 165 166 void initializeLoopUnswitchPass(PassRegistry&); 166 167 void initializeLoopIdiomRecognizePass(PassRegistry&);
 90 90 (void) llvm::createLoopExtractorPass(); 91 91 (void) llvm::createLoopSimplifyPass(); 92 92 (void) llvm::createLoopStrengthReducePass(); 93 (void) llvm::createLoopRerollPass(); 93 94 (void) llvm::createLoopUnrollPass(); 94 95 (void) llvm::createLoopUnswitchPass(); 95 96 (void) llvm::createLoopIdiomPass();
 144 144 145 145 //===----------------------------------------------------------------------===// 146 146 // 147 // LoopReroll - This pass is a simple loop rerolling pass. 148 // 149 Pass *createLoopRerollPass(); 150 151 //===----------------------------------------------------------------------===// 152 // 147 153 // LoopRotate - This pass is a simple loop rotating pass. 148 154 // 149 155 Pass *createLoopRotatePass();
 63 63 64 64 /** See llvm::createLoopRotatePass function. */ 65 65 void LLVMAddLoopRotatePass(LLVMPassManagerRef PM); 66 67 /** See llvm::createLoopRerollPass function. */ 68 void LLVMAddLoopRerollPass(LLVMPassManagerRef PM); 66 69 67 70 /** See llvm::createLoopUnrollPass function. */ 68 71 void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM);
 53 53 cl::init(true), cl::Hidden, 54 54 cl::desc("Enable the new, experimental SROA pass")); 55 55 56 static cl::opt 57 RunLoopRerolling("reroll-loops", cl::Hidden, 58 cl::desc("Run the loop rerolling pass")); 59 56 60 PassManagerBuilder::PassManagerBuilder() { 57 61 OptLevel = 2; 58 62 SizeLevel = 0; 215 219 216 220 addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 217 221 222 if (RunLoopRerolling) 223 MPM.add(createLoopRerollPass()); 218 224 if (SLPVectorize) 219 225 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 220 226
 15 15 LoopInstSimplify.cpp 16 16 LoopRotation.cpp 17 17 LoopStrengthReduce.cpp 18 LoopRerollPass.cpp 18 19 LoopUnrollPass.cpp 19 20 LoopUnswitch.cpp 20 21 LowerAtomic.cpp