00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 # include "incls/_precompiled.incl"
00025
00026 # ifdef DELTA_COMPILER
00027
00028 # include "incls/_defUse.cpp.incl"
00029
00030
00031 void DUInfo::propagateTo(BB* useBB, Use* use, const NonTrivialNode* fromNode,
00032 PReg* src, NonTrivialNode* toNode, const bool global) {
00033
00034 bool ok = toNode->copyPropagate(useBB, use, src);
00035 if (CompilerDebug) {
00036 cout(PrintCopyPropagation)->print("*%s cp:%s propagate %s from N%ld (%#lx) to N%ld (%#lx)\n",
00037 global ? "global" : "local", ok ? "" : " couldn't",
00038 src->name(), fromNode ? fromNode->id() : 0, PrintHexAddresses ? fromNode : 0,
00039 toNode->id(), PrintHexAddresses ? toNode : 0);
00040 }
00041 }
00042
00043 void DUInfo::propagateTo(BB* useBB, const PReg* r, const Def* def, Use* use,
00044 const bool global) {
00045
00046 NonTrivialNode* fromNode = def->node;
00047 const bool isAssignment = fromNode->isAssignmentLike();
00048 NonTrivialNode* toNode = use->node;
00049 const bool hasSrc = fromNode->hasSrc();
00050 PReg* fromPR = hasSrc ? fromNode->src() : NULL;
00051 const bool isConst = hasSrc && fromPR->isConstPReg();
00052
00053 if (isAssignment && isConst && toNode->canCopyPropagateOop()) {
00054
00055
00056 bool ok = toNode->copyPropagate(useBB, use, fromPR);
00057 if (CompilerDebug) {
00058
00059 char* glob = global ? "global" : "local";
00060 char* prop = ok ? "" : " couldn't propagate";
00061 char* name = def->node->src()->name();
00062 void* from = PrintHexAddresses ? fromNode : 0;
00063 void* to = PrintHexAddresses ? toNode : 0;
00064 cout(PrintCopyPropagation)->print("*%s cp:%s %s from N%ld (%#lx) to N%ld (%#lx)\n",
00065 glob, prop, name, def->node->id(), from, toNode->id(), to);
00066 }
00067 return;
00068 }
00069
00070 if (fromNode->loopDepth() != toNode->loopDepth()) {
00071
00072
00073
00074
00075 assert(global, "can't be local cp");
00076 if (CompilerDebug) {
00077 cout(PrintCopyPropagation)->print("*global cp: can't propagate %s from N%ld (%#lx) to N%ld (%#lx) -- loopDepth mismatch\n",
00078 fromPR->name(), fromNode->id(), PrintHexAddresses ? fromNode : 0, toNode->id(), PrintHexAddresses ? toNode : 0);
00079 }
00080 return;
00081 }
00082
00083 if (isAssignment && !isConst && hasSrc && toNode->canCopyPropagate(fromNode)) {
00084
00085 propagateTo(useBB, use, fromNode, fromPR, toNode, global);
00086 return;
00087 }
00088
00089 if (!global && r->isSinglyUsed() && (toNode->isAssignNode() || toNode->isInlinedReturnNode()) &&
00090 toNode->canBeEliminated() && fromNode->hasDest() && fromNode->canChangeDest() &&
00091 r->canBeEliminated(true) && !toNode->dest()->loc.isTopOfStack()) {
00092
00093
00094
00095
00096
00097 if (CompilerDebug) {
00098 cout(PrintCopyPropagation)->print("*%s cp: changing dest of N%ld (%#lx) to %s to match use at N%ld (%#lx)\n",
00099 global ? "global" : "local",
00100 fromNode->id(), PrintHexAddresses ? fromNode : 0, toNode->dest()->name(), toNode->id(), PrintHexAddresses ? toNode : 0);
00101 }
00102 assert(!r->incorrectDU(), "shouldn't try CP on this");
00103 assert(!global || r->isSinglyAssigned(), "not safe with >1 defs");
00104 assert(fromNode->dest() == r, "unexpected dest");
00105 fromNode->setDest(fromNode->bb(), toNode->dest());
00106 toNode->eliminate(toNode->bb(), NULL, false, true);
00107 return;
00108 }
00109
00110
00111 if (CompilerDebug) {
00112 cout(PrintCopyPropagation)->print("*%s cp: can't propagate N%ld (%#lx) to N%ld (%#lx)\n",
00113 global ? "global" : "local", fromNode->id(), PrintHexAddresses ? fromNode : 0,
00114 toNode->id(), PrintHexAddresses ? toNode : 0);
00115 }
00116 }
00117
00118 void Use::print() { lprintf("Use %#lx (N%ld)", PrintHexAddresses ? this : 0, node->id()); }
00119 void PSoftUse::print() { lprintf("PSoftUse %#lx (N%ld)", PrintHexAddresses ? this : 0, node->id());}
00120 void Def::print() { lprintf("Def %#lx (N%ld)", PrintHexAddresses ? this : 0, node->id()); }
00121
00122 void BBDUTable::print() {
00123 if (!info) return;
00124 print_short();
00125 for (int i = 0; i < info->length(); i++) {
00126 lprintf("%3ld: ", i); info->at(i)->print();
00127 }
00128 }
00129
00130 void DUInfo::getLiveRange(int& firstNodeNum, int& lastNodeNum) {
00131
00132
00133
00134
00135
00136
00137
00138 SListElem<Def*>* d = defs.head();
00139 SListElem<Use*>* u = uses.head();
00140
00141
00142 if (u && d) {
00143 int unode = u->data()->node->num();
00144 int dnode = d->data()->node->num();
00145 if (unode > dnode) {
00146 firstNodeNum = dnode;
00147 } else {
00148
00149 assert(firstNodeNum == 0, "should be zero");
00150 }
00151 } else if (u) {
00152
00153 assert(firstNodeNum == 0, "should be zero");
00154 while (u && u->next()) u = u->next();
00155 lastNodeNum = u->data()->node->num();
00156 return;
00157 } else if (d) {
00158 firstNodeNum = d->data()->node->num();
00159 return;
00160 } else {
00161
00162
00163 assert(reg->loc == resultLoc, "must be result loc");
00164 firstNodeNum = lastNodeNum = 0;
00165 return;
00166 }
00167
00168
00169 while (u && u->next()) u = u->next();
00170 while (d && d->next()) d = d->next();
00171 int unode = u->data()->node->num();
00172 int dnode = d->data()->node->num();
00173 if (unode > dnode) {
00174 lastNodeNum = unode;
00175 } else {
00176
00177 }
00178 }
00179
00180 void DUInfo::print_short() { lprintf("DUInfo %#lx", this); }
00181 void DUInfo::print() {
00182 print_short(); lprintf(" for "); reg->print_short(); lprintf(": ");
00183 uses.print(); lprintf("; "); defs.print(); lprintf("\n");
00184 }
00185
00186 void PRegBBIndex::print_short() {
00187 lprintf("PRegBBIndex [%ld] for", index); bb->print_short();
00188 }
00189
00190 void PRegBBIndex::print() {
00191 print_short(); lprintf(": "); bb->duInfo.info->at(index)->print();
00192 }
00193
00194 static bool cpCreateFailed = false;
00195
00196 CPInfo* new_CPInfo(NonTrivialNode* n) {
00197 CPInfo* cpi = new CPInfo(n);
00198 if (cpCreateFailed) {
00199 cpCreateFailed = false;
00200 return NULL;
00201 }
00202 return cpi;
00203 }
00204
00205 CPInfo::CPInfo(NonTrivialNode* n) {
00206 def = n;
00207 if (!n->hasSrc()) {
00208 cpCreateFailed = true;
00209 return;
00210 }
00211 r = n->src();
00212 if (r->isConstPReg()) return;
00213
00214 PReg* eliminatee = n->dest();
00215 if (eliminatee->debug) {
00216 if (r->extendLiveRange(eliminatee->scope(), eliminatee->endBCI())) {
00217
00218
00219 } else {
00220 cpCreateFailed = true;
00221 }
00222 }
00223 }
00224
00225 bool CPInfo::isConstant() const { return r->isConstPReg(); }
00226 oop CPInfo::constant() const {
00227 assert(isConstant(), "not constant");
00228 return ((ConstPReg*)r)->constant;
00229 }
00230
00231 void CPInfo::print() {
00232 lprintf("*(CPInfo*)%#x : def %#x, %s\n", this, def, r->name());
00233 }
00234
00235 static void printNodeFn(DefUse* du) { lprintf("N%d ", du->node->id()); }
00236
00237 struct PrintNodeClosureD : public Closure<Def*> {
00238 void do_it(Def* d) { printNodeFn(d); }
00239 };
00240
00241 struct PrintNodeClosureU : public Closure<Use*> {
00242 void do_it(Use* u) { printNodeFn(u); }
00243 };
00244
00245 void printDefsAndUses(const GrowableArray<PRegBBIndex*>* l) {
00246 lprintf("defs: ");
00247 PrintNodeClosureD printNodeD;
00248 forAllDefsDo(l, &printNodeD);
00249 lprintf("; uses: ");
00250 PrintNodeClosureU printNodeU;
00251 forAllUsesDo(l, &printNodeU);
00252 }
00253
00254
00255
00256 static Closure<Def*>* theDefIterator;
00257 static void doDefsFn(PRegBBIndex* p) {
00258 DUInfo* info = p->bb->duInfo.info->at(p->index);
00259 info->defs.apply(theDefIterator);
00260 }
00261
00262 void forAllDefsDo(const GrowableArray<PRegBBIndex*>* l, Closure<Def*>* f) {
00263 theDefIterator = f;
00264 l->apply(doDefsFn);
00265 }
00266
00267 static Closure<Use*>* theUseIterator;
00268 static void doUsesFn(PRegBBIndex* p) {
00269 DUInfo* info = p->bb->duInfo.info->at(p->index);
00270 info->uses.apply(theUseIterator);
00271 }
00272
00273 void forAllUsesDo(const GrowableArray<PRegBBIndex*>* l, Closure<Use*>* f) {
00274 theUseIterator = f;
00275 l->apply(doUsesFn);
00276 }
00277
00278 # endif