summaryrefslogtreecommitdiff
path: root/polly/lib/CodeGen/BlockGenerators.cpp
AgeCommit message (Collapse)Author
2015-11-09[NFC] Remove unused variable.Johannes Doerfert
llvm-svn: 252436
2015-11-07Fix non-affine region dominance of implicitely stored valuesMichael Kruse
After loop versioning, a dominance check of a non-affine subregion's exit node causes the dominance check to always fail on any block in the subregion if it shares the same exit block with the scop. The subregion's exit block has become polly_merge_new_and_old, which also receives the control flow of the generated code. This would cause that any value for implicit stores is assumed to be not from the scop. We check dominance with the generated exit node instead. This fixes llvm.org/PR25438 llvm-svn: 252375
2015-11-06polly/ADT: Remove implicit ilist iterator conversions, NFCDuncan P. N. Exon Smith
Remove all the implicit ilist iterator conversions from polly, in preparation for making them illegal in ADT. There was one oddity I came across: at line 95 of lib/CodeGen/LoopGenerators.cpp, there was a post-increment `Builder.GetInsertPoint()++`. Since it was a no-op, I removed it, but I admit I wonder if it might be a bug (both before and after this change)? Perhaps it should be a pre-increment? llvm-svn: 252357
2015-11-06Fix reuse of non-dominating synthesized value in subregion exitMichael Kruse
We were adding all generated values in non-affine subregions to be used for the subregions generated exit block. The thought was that only values that are dominating the original exit block can be used there. But it is possible for synthesizable values to be expanded in any block. If the same values is also used for implicit writes, it would try to reuse already synthesized values even if not dominating the exit block. The fix is to only add values to the list of values usable in the exit block only if it is dominating the exit block. This fixes llvm.org/PR25412. llvm-svn: 252301
2015-11-05Use per-BB value maps for non-exit BBsMichael Kruse
For generating scalar writes of non-affine subregions, all except phi writes are generated in the exit block. The phi writes are generated in the incoming block for which we errornously used the same BBMap. This can conflict if a value for one block is synthesized, and then reused for another block which is not dominated by the first block. This is fixed by using block-specific BBMaps for phi writes. llvm-svn: 252172
2015-10-26RegionGenerator: Clear local maps after statement constructionTobias Grosser
These maps are only needed during the construction of a single region statement. Clearing them is important, as we otherwise get an assert in case some of the referenced values are erased before the RegionGenerator is deleted. llvm-svn: 251341
2015-10-24BlockGenerator: Do not assert when finding model PHI nodes defined outside ↵Tobias Grosser
the scop Such PHI nodes can not only appear in the ExitBlock of the Scop, but indeed any scalar PHI node above the scop and used in the scop is modeled as scalar read access. llvm-svn: 251198
2015-10-24BlockGenerator: Directly handle multi-exit PHI nodesTobias Grosser
This change adds code to directly code-generate multi-exit PHI nodes, instead of trying to reuse the EscapeMap infrastructure for this. Using escape maps adds a level of indirection that is hard to understand and - more importantly - breaks in certain cases. Specifically, the original code relied on simplifyRegion() to split the original PHI node in two PHI nodes, one merging the values coming from within the scop and a second that merges the first PHI node with the values that come from outside the scop. To generate code the first PHI node is then just handled like any other in-scop value that is used somewhere outside the scop. This fails for the case where all values from inside the scop are identical, as the first PHI node is in such cases automatically simplified and eliminated by LLVM right at construction. As a result, there is no instruction that can be pass to the EscapeMap handling, which means the references in the second PHI node are not updated and may still reference values from within the original scop that do not dominate it. Our new code iterates directly over all modeled ScopArrayInfo objects that represent multi-exit PHI nodes and generates code for them without relying on the EscapeMap infrastructure. Hence, it works also for the case where the first PHI node is eliminated. llvm-svn: 251191
2015-10-19Synthesize phi arguments in incoming blockMichael Kruse
New values were always synthesized in the block of the instruction that needed them. This is incorrect for PHI node whose' value must be defined in the respective incoming block. This patch temporarily moves the builder's insert point to the incoming block while synthesizing phi node arguments. This fixes PR25241 (http://llvm.org/bugs/show_bug.cgi?id=25241) llvm-svn: 250693
2015-10-18[FIX] Cast preloaded valuesJohannes Doerfert
Preloaded values have to match the type of their counterpart in the original code and not the type of the base array. llvm-svn: 250654
2015-10-18Revert to original BlockGenerator::getOrCreateAlloca(MemoryAccess &Access)Tobias Grosser
Expressing this in terms of BlockGenerator::getOrCreateAlloca(const ScopArrayInfo *Array) does not work as the MemoryAccess BasePtr is in case of invariant load hoisting different to the ScopArrayInfo BasePtr. Until this is investigated and fixed, we move back to code that just uses the baseptr of MemoryAccess. llvm-svn: 250637
2015-10-17BlockGenerator: Add getOrCreateAlloca(const ScopArrayInfo *Array)Tobias Grosser
This allows the caller to get the alloca locations of an array without the need to thank if Array is a PHI or a non-PHI Array. We directly make use of this in BlockGenerator::getOrCreateAlloca(MemoryAccess &Access). llvm-svn: 250628
2015-10-17Load/Store scalar accesses before/after the statement itselfMichael Kruse
Instead of generating implicit loads within basic blocks, put them before the instructions of the statment itself, including non-affine subregions. The region's entry node is dominating all blocks in the region and therefore the loaded value will be available there. Implicit writes in block-stmts were already stored back at the end of the block. Now, also generate the stores of non-affine subregions when leaving the statement, i.e. in the exiting block. This change is required for array-mapped implicits ("De-LICM") to ensure that there are no dependencies of demoted scalars within statments. Statement load all required values, operator on copied in registers, and then write back the changed value to the demoted memory. Lifetimes analysis within statements becomes unecessary. Differential Revision: http://reviews.llvm.org/D13487 llvm-svn: 250625
2015-10-17BlockGenerator: Register outside users of scalars directlyTobias Grosser
Instead of checking at code generation time for each ScopStmt if a scalar has external uses, we just iterate over the ScopArrayInfo descriptions we have and check each of these for possible external uses. Besides being somehow clearer, this approach has the benefit that we will always create valid LLVM-IR even in case we disable the code generation of ScopStmt bodies e.g. for testing purposes. llvm-svn: 250608
2015-10-17Drop unused parameter from handleOutsideUsersTobias Grosser
llvm-svn: 250606
2015-10-09[NFC] Move helper functions to ScopHelperJohannes Doerfert
Helper functions in the BlockGenerators.h/cpp introduce dependences from the frontend to the backend of Polly. As they are used in ScopDetection, ScopInfo, etc. we move them to the ScopHelper file. llvm-svn: 249919
2015-10-07Allow invariant loads in the SCoP descriptionJohannes Doerfert
This patch allows invariant loads to be used in the SCoP description, e.g., as loop bounds, conditions or in memory access functions. First we collect "required invariant loads" during SCoP detection that would otherwise make an expression we care about non-affine. To this end a new level of abstraction was introduced before SCEVValidator::isAffineExpr() namely ScopDetection::isAffine() and ScopDetection::onlyValidRequiredInvariantLoads(). Here we can decide if we want a load inside the region to be optimistically assumed invariant or not. If we do, it will be marked as required and in the SCoP generation we bail if it is actually not invariant. If we don't it will be a non-affine expression as before. At the moment we optimistically assume all "hoistable" (namely non-loop-carried) loads to be invariant. This causes us to expand some SCoPs and dismiss them later but it also allows us to detect a lot we would dismiss directly if we would ask e.g., AliasAnalysis::canBasicBlockModify(). We also allow potential aliases between optimistically assumed invariant loads and other pointers as our runtime alias checks are sound in case the loads are actually invariant. Together with the invariant checks this combination allows to handle a lot more than LICM can. The code generation of the invariant loads had to be extended as we can now have dependences between parameters and invariant (hoisted) loads as well as the other way around, e.g., test/Isl/CodeGen/invariant_load_parameters_cyclic_dependence.ll First, it is important to note that we cannot have real cycles but only dependences from a hoisted load to a parameter and from another parameter to that hoisted load (and so on). To handle such cases we materialize llvm::Values for parameters that are referred by a hoisted load on demand and then materialize the remaining parameters. Second, there are new kinds of dependences between hoisted loads caused by the constraints on their execution. If a hoisted load is conditionally executed it might depend on the value of another hoisted load. To deal with such situations we sort them already in the ScopInfo such that they can be generated in the order they are listed in the Scop::InvariantAccesses list (see compareInvariantAccesses). The dependences between hoisted loads caused by indirect accesses are handled the same way as before. llvm-svn: 249607
2015-10-04BlockGenerator: Use plain Value * instead of const Value *Tobias Grosser
The use of const qualified Value pointers prevents the use of AssertingVH. We could probably think of adding const support to AssertingVH, but as const correctness seems to currently provide limited benefit in Polly, we do not do this yet. llvm-svn: 249266
2015-10-04BlockGenerators: Use auto to be less sensitive to type changesTobias Grosser
llvm-svn: 249265
2015-10-04Consolidate the different ValueMapTypes we are usingTobias Grosser
There have been various places where llvm::DenseMap<const llvm::Value *, llvm::Value *> types have been defined, but all types have been expected to be identical. We make this more clear by consolidating the different types and use BlockGenerator::ValueMapT wherever there is a need for types to match BlockGenerator::ValueMapT. llvm-svn: 249264
2015-10-03BlockGenerator: Use AssertingVH in mapsTobias Grosser
By using asserting value handles, we will get assertions when we forget to clear any of the Value maps instead of difficult to debug undefined behavior. llvm-svn: 249237
2015-09-30Move remapping functionality in the ScopExpanderJohannes Doerfert
Because we handle more than SCEV does it is not possible to rewrite an expression on the top-level using the SCEVParameterRewriter only. With this patch we will do the rewriting on demand only and also recursively, thus not only on the top-level. llvm-svn: 248916
2015-09-30Reapply "BlockGenerator: Generate synthesisable instructions only on-demand"Tobias Grosser
Instructions which we can synthesis from a SCEV expression are not generated directly, but only when they are used as an operand of another instruction. This avoids generating unnecessary instructions and works more reliably than first inserting them and then deleting them later on. This commit was reverted in r248860 due to a remaining miscompile, where we forgot to synthesis the operand values that were referenced from scalar writes. test/Isl/CodeGen/scalar-store-from-same-bb.ll tests that we do this now correctly. llvm-svn: 248900
2015-09-30BlockGenerator: Extract value synthesis into its own function [NFC]Tobias Grosser
This will allow us to reuse this code in a subsequent commit. llvm-svn: 248893
2015-09-29Identify and hoist definitively invariant loadsJohannes Doerfert
As a first step in the direction of assumed invariant loads (loads that are not written in some context) we now detect and hoist definitively invariant loads. These invariant loads will be preloaded in the code generation and used in the optimized version of the SCoP. If the load is only conditionally executed the preloaded version will also only be executed under the same condition, hence we will never access memory that wouldn't have been accessed otherwise. This is also the most distinguishing feature to licm. As hoisting can make statements empty we will simplify the SCoP and remove empty statements that would otherwise cause artifacts in the code generation. Differential Revision: http://reviews.llvm.org/D13194 llvm-svn: 248861
2015-09-29Revert "BlockGenerator: Generate synthesisable instructions only on-demand"Johannes Doerfert
This reverts commit 07830c18d789ee72812d5b5b9b4f8ce72ebd4207. The commit broke at least one test in lnt, MultiSource/Benchmarks/Ptrdist/bc/number.c was miss compiled and the test produced a wrong result. One Polly test case that was added later was adjusted too. llvm-svn: 248860
2015-09-29Codegen: Support memory accesses with different typesTobias Grosser
Every once in a while we see code that accesses memory with different types, e.g. to perform operations on a piece of memory using type 'float', but to copy data to this memory using type 'int'. Modeled in C, such codes look like: void foo(float A[], float B[]) { for (long i = 0; i < 100; i++) *(int *)(&A[i]) = *(int *)(&B[i]); for (long i = 0; i < 100; i++) A[i] += 10; } We already used the correct types during normal operations, but fall back to our detected type as soon as we import changed memory access functions. For these memory accesses we may generate invalid IR due to a mismatch between the element type of the array we detect and the actual type used in the memory access. To address this issue, we always cast the newly created address of a memory access back to the type of the memory access where the address will be used. llvm-svn: 248781
2015-09-28BlockGenerator: Generate synthesisable instructions only on-demandTobias Grosser
Instructions which we can synthesis from a SCEV expression are not generated directly, but only when they are used as an operand of another instruction. This avoids generating unnecessary instruction and works more reliably than first inserting them and then deleting them later on. Suggested-by: Johannes Doerfert <doerfert@cs.uni-saarland.de> Differential Revision: http://reviews.llvm.org/D13208 llvm-svn: 248712
2015-09-28Allow switch instructions in SCoPsJohannes Doerfert
This patch allows switch instructions with affine conditions in the SCoP. Also switch instructions in non-affine subregions are allowed. Both did not require much changes to the code, though there was some refactoring needed to integrate them without code duplication. In the llvm-test suite the number of profitable SCoPs increased from 135 to 139 but more importantly we can handle more benchmarks and user inputs without preprocessing. Differential Revision: http://reviews.llvm.org/D13200 llvm-svn: 248701
2015-09-27BlockGenerator: Be less agressive with deleting dead instructionsTobias Grosser
We now only delete trivially dead instructions in the BB we copy (copyBB), but not in any other BB. Only for copyBB we know that there will _never_ be any future uses of instructions that have no use after copyBB has been generated. Other instructions in the AST that have been generated by IslNodeBuilder may look dead at the moment, but may possibly still be referenced by GlobalMaps. If we delete them now, later uses would break surprisingly. We do not have a test case that breaks due to us deleting too many instructions. This issue was found by inspection. llvm-svn: 248688
2015-09-27BlockGenerator: Simplify code generated for region statementsTobias Grosser
After having generated a new user statement a couple of inefficient or trivially dead instructions may remain. This commit runs instruction simplification over the newly generated blocks to ensure unneeded instructions are removed right away. This commit does adds simplification for non-affine subregions which was not yet part of 248681. llvm-svn: 248683
2015-09-27BlockGenerator: Simplify code generated for scop statementsTobias Grosser
After having generated a new user statement a couple of inefficient or trivially dead instructions may remain. This commit runs instruction simplification over the newly generated blocks to ensure unneeded instructions are removed right away. This commit does not yet add simplification for non-affine subregions. llvm-svn: 248681
2015-09-26Create parallel code in a separate blockJohannes Doerfert
This commit basically reverts r246427 but still solves the issue tackled by that commit. Instead of emitting initialization code in the beginning of the start block we now generate parallel code in its own block and thereby guarantee separation. This is necessary as we cannot generate code for hoisted loads prior to the start block but it still needs to be placed prior to everything else. llvm-svn: 248674
2015-09-25Let MemoryAccess remember its purposeMichael Kruse
There are three possible reasons to add a memory memory access: For explicit load and stores, for llvm::Value defs/uses, and to emulate PHI nodes (the latter two called implicit accesses). Previously MemoryAccess only stored IsPHI. Register accesses could be identified through the isScalar() method if it was no IsPHI. isScalar() determined the number of dimensions of the underlaying array, scalars represented by zero dimensions. For the work on de-LICM, implicit accesses can have more than zero dimensions, making the distinction of isScalars() useless, hence now stored explicitly in the MemoryAccess. Instead, we replace it by isImplicit() and avoid the term scalar for zero-dimensional arrays as it might be confused with llvm::Value which are also often referred to as scalars (or alternatively, as registers). No behavioral change intended, under the condition that it was impossible to create explicit accesses to zero-dimensional "arrays". llvm-svn: 248616
2015-09-18Merge IRAccess into MemoryAccessMichael Kruse
All MemoryAccess objects will be owned by ScopInfo::AccFuncMap which previously stored the IRAccess objects. Instead of creating new MemoryAccess objects, the already created ones are reused, but their order might be different now. Some fields of IRAccess and MemoryAccess had the same meaning and are merged. This is the last step of fusioning TempScopInfo.{h|cpp} and ScopInfo.{h.cpp}. Some refactoring might still make sense. Differential Revision: http://reviews.llvm.org/D12843 llvm-svn: 248024
2015-09-18Store EscapeMap as Value* instead of AllocInstTobias Grosser
This currently does not change the behavior in Polly, but it allows us to later also overwrite the EscapeMap with our GlobalMap. llvm-svn: 247970
2015-09-14[FIX] Handle error blocks in non-affine regions correctlyJohannes Doerfert
llvm-svn: 247545
2015-09-08Allow PHI nodes in the region exit blockJohannes Doerfert
While we do not need to model PHI nodes in the region exit (as it is not part of the SCoP), we need to prepare for the case that the exit block is split in code generation to create a single exiting block. If this will happen, hence if the region did not have a single exiting block before, we will model the operands of the PHI nodes as escaping scalars in the SCoP. Differential Revision: http://reviews.llvm.org/D12051 llvm-svn: 247078
2015-09-06Add option -polly-codegen-add-debug-printingTobias Grosser
When this option is enabled, Polly will emit printf calls for each scalar load/and store which dump the scalar value loaded/stored at run time. This patch also refactors the RuntimeDebugBuilder to use variadic templates when generating CPU printfs. As result, it now becomes easier to print strings that consist of a set of arguments. Also, as a single printf call is emitted, it is more likely for such strings to be emitted atomically if executed multi-threaded. llvm-svn: 246941
2015-09-05RegionGenerator: Do not modify GlobalMapsTobias Grosser
By inspection the update of the GlobalMaps in the RegionGenerator seems unneed, and is removed as also no test cases fail when dropping this. Johannes Doerfert confirmed that this is indeed save: "I think that code was needed when we did not use the scalar codegen by default. Now everything defined in a non-affine region should be communicated via memory and reloaded in the user block. Hence, we should be good removing this code." llvm-svn: 246926
2015-09-05BlockGenerator: Make GlobalMap a member variableTobias Grosser
The GlobalMap variable used in BlockGenerator should always reference the same list througout the entire code generation, hence we can make it a member variable to avoid passing it around through every function call. History: Before we switched to the SCEV based code generation the GlobalMap also contained a mapping form old to new induction variables, hence it was different for each ScopStmt, which is why we passed it as function argument to copyStmt. The new SCEV based code generation now uses a separate mapping called LTS -> LoopToSCEV that maps each original loop to a new loop iteration variable provided as a SCEVExpr. The GlobalMap is currently mostly used for OpenMP code generation, where references to parameters in the original function need to be rewritten to the locations of these variables after they have been passed to the subfunction. Suggested-by: Johannes Doerfert <doerfert@cs.uni-saarland.de> llvm-svn: 246920
2015-08-31Generate scalar initialization loads at the beginning of the start BBTobias Grosser
Our OpenMP code generation generated part of its launching code directly into the start basic block and without this change the scalar initialization was run _after_ the OpenMP threads have been launched. This resulted in uninitialized scalar values to be used. llvm-svn: 246427
2015-08-31Add support for scalar dependences to OpenMP code generationTobias Grosser
Scalar dependences between scop statements have caused troubles during parallel code generation as we did not pass on the new stack allocation created for such scalars to the parallel subfunctions. This change now detects all scalar reads/writes in parallel subfunctions, creates the allocas for these scalar objects, passes the resulting memory locations to the subfunctions and ensures that within the subfunction requests for these memory locations will return the rewritten values. Johannes suggested as a future optimization to privatizing some of the scalars in the subfunction. llvm-svn: 246414
2015-08-30Do not store into a temporary twineTobias Grosser
For some reason, this causes memory corruption issues. Let's just avoid it. llvm-svn: 246396
2015-08-30Store scalar dependences from outside the scop into alloca locationsTobias Grosser
We already modeled read-only dependences to scalar values defined outside the scop as memory reads and also generated read accesses from the corresponding alloca instructions that have been used to pass these scalar values around during code generation. However, besides for PHI nodes that have already been handled, we failed to store the orignal read-only scalar values into these alloc. This commit extends the initialization of scalar values to all read-only scalar values used within the scop. llvm-svn: 246394
2015-08-30getNewScalarValue: Get ScalarMap directly from member variable [NFC]Tobias Grosser
There is no need to pass the ScalarMap to getNewScalarValue as this map is (indirectly) used when calling getOrCreateScalarAlloca. llvm-svn: 246390
2015-08-30createScalarInitialization: Always store PHI-node valueTobias Grosser
The current code really tries hard to use getNewScalarValue(), which checks if not the original value, but a possible copy or demoted value needs to be stored. In this calling context it seems, that we _always_ use the ScalarValue that comes from the incoming PHI node, but never any other value. As also no test cases fail, it seems right to just drop this call to getNewScalarValue and remove the parameters that are not needed any more. Johannes suggested that code like this might be needed for parallel code generation with offloading, but it was still unclear if/what exactly would be needed. As the parallel code generation does currently not support scalars at all, we will remove this code for now and add relevant code back when complitng the support of scalars in the parallel code generation. Reviewers: jdoerfert Subscribers: pollydev, llvm-commits Differential Revision: http://reviews.llvm.org/D12470 llvm-svn: 246389
2015-08-30Ignore debug intrinsics and do not model their potential scalar metadata readsTobias Grosser
Our code generation currently does not support scalar references to metadata values. Hence, it would crash if we try to model scalar dependences to metadata values. Fortunately, for one of the common uses, debug information, we can for now just ignore the relevant intrinsics and consequently the issue of how to model scalar dependences to metadata. llvm-svn: 246388
2015-08-30Remove some code duplication [NFC]Tobias Grosser
llvm-svn: 246387
2015-08-30Minor code style improvement [NFC]Tobias Grosser
llvm-svn: 246386