Codegen: Copy and restore the ValueMap and ClastVars explicitly
When generating OpenMP or GPGPU code the original ValueMap and ClastVars must be
kept. We already recovered the original ClastVars by reverting the changes, but
we did not keep the content of the ValueMap. This patch keeps now an explicit
copy of both maps and restores them after generating OpenMP or GPGPU code.
This is an adapted version of a patch contributed by:
Armin Groesslinger <armin.groesslinger@uni-passau.de>
llvm-svn: 167213
diff --git a/polly/lib/CodeGen/CodeGeneration.cpp b/polly/lib/CodeGen/CodeGeneration.cpp
index fcd2d52..d713ae47 100644
--- a/polly/lib/CodeGen/CodeGeneration.cpp
+++ b/polly/lib/CodeGen/CodeGeneration.cpp
@@ -285,12 +285,10 @@
/// structure.
SetVector<Value*> getOMPValues();
- /// @brief Update the internal structures according to a Value Map.
+ /// @brief Update ClastVars and ValueMap according to a value map.
///
- /// @param VMap A map from old to new values.
- /// @param Reverse If true, we assume the update should be reversed.
- void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
- bool Reverse);
+ /// @param VMap A map from old to new values.
+ void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap);
/// @brief Create an OpenMP parallel for loop.
///
@@ -484,37 +482,18 @@
return Values;
}
-void ClastStmtCodeGen::updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
- bool Reverse) {
+void ClastStmtCodeGen::updateWithValueMap(
+ OMPGenerator::ValueToValueMapTy &VMap) {
std::set<Value*> Inserted;
- if (Reverse) {
- OMPGenerator::ValueToValueMapTy ReverseMap;
-
- for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
- I != E; ++I)
- ReverseMap.insert(std::make_pair(I->second, I->first));
-
- for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
- I != E; I++) {
- ClastVars[I->first] = ReverseMap[I->second];
- Inserted.insert(I->second);
- }
-
- /// FIXME: At the moment we do not reverse the update of the ValueMap.
- /// This is incomplet, but the failure should be obvious, such that
- /// we can fix this later.
- return;
- }
-
for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
I != E; I++) {
ClastVars[I->first] = VMap[I->second];
Inserted.insert(I->second);
}
- for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
- I != E; ++I) {
+ for (OMPGenerator::ValueToValueMapTy::iterator I = VMap.begin(),
+ E = VMap.end(); I != E; ++I) {
if (Inserted.count(I->first))
continue;
@@ -552,14 +531,19 @@
BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
Builder.SetInsertPoint(LoopBody);
- updateWithValueMap(VMap, /* reverse */ false);
+ // Save the current values.
+ const ValueMapT ValueMapCopy = ValueMap;
+ const CharMapT ClastVarsCopy = ClastVars;
+
+ updateWithValueMap(VMap);
ClastVars[For->iterator] = IV;
if (For->body)
codegen(For->body);
- ClastVars.erase(For->iterator);
- updateWithValueMap(VMap, /* reverse */ true);
+ // Restore the original values.
+ ValueMap = ValueMapCopy;
+ ClastVars = ClastVarsCopy;
clearDomtree((*LoopBody).getParent()->getParent(),
P->getAnalysis<DominatorTree>());
@@ -698,9 +682,16 @@
VMap.insert(std::make_pair<Value*, Value*>(OldIV, IV));
}
- updateWithValueMap(VMap, /* reverse */ false);
+ // Preserve the current values.
+ const ValueMapT ValueMapCopy = ValueMap;
+ const CharMapT ClastVarsCopy = ClastVars;
+ updateWithVMap(VMap);
+
BlockGenerator::generate(Builder, *Statement, ValueMap, P);
- updateWithValueMap(VMap, /* reverse */ true);
+
+ // Restore the original values.
+ ValueMap = ValueMapCopy;
+ ClastVars = ClastVarsCopy;
if (AfterBB)
Builder.SetInsertPoint(AfterBB->begin());