Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: src/IceInstMIPS32.cpp

Issue 2275883002: [SubZero] Branch optimization (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Tests added for branch optimization. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceInstMIPS32.h ('k') | src/IceTargetLoweringMIPS32.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceInstMips32.cpp - Mips32 instruction implementation --===// 1 //===- subzero/src/IceInstMips32.cpp - Mips32 instruction implementation --===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 /// \file 10 /// \file
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 InstMIPS32Br::InstMIPS32Br(Cfg *Func, const CfgNode *TargetTrue, 193 InstMIPS32Br::InstMIPS32Br(Cfg *Func, const CfgNode *TargetTrue,
194 const CfgNode *TargetFalse, Operand *Src0, 194 const CfgNode *TargetFalse, Operand *Src0,
195 Operand *Src1, const InstMIPS32Label *Label, 195 Operand *Src1, const InstMIPS32Label *Label,
196 CondMIPS32::Cond Cond) 196 CondMIPS32::Cond Cond)
197 : InstMIPS32(Func, InstMIPS32::Br, 2, nullptr), TargetTrue(TargetTrue), 197 : InstMIPS32(Func, InstMIPS32::Br, 2, nullptr), TargetTrue(TargetTrue),
198 TargetFalse(TargetFalse), Label(Label), Predicate(Cond) { 198 TargetFalse(TargetFalse), Label(Label), Predicate(Cond) {
199 addSource(Src0); 199 addSource(Src0);
200 addSource(Src1); 200 addSource(Src1);
201 } 201 }
202 202
203 CondMIPS32::Cond InstMIPS32::getOppositeCondition(CondMIPS32::Cond Cond) {
204 return InstMIPS32CondAttributes[Cond].Opposite;
205 }
206
207 bool InstMIPS32Br::optimizeBranch(const CfgNode *NextNode) {
208 // If there is no next block, then there can be no fallthrough to optimize.
209 if (NextNode == nullptr)
210 return false;
211 // Intra-block conditional branches can't be optimized.
212 if (Label != nullptr)
213 return false;
214 // Unconditional branch to the next node can be removed.
215 if (isUnconditionalBranch() && getTargetFalse() == NextNode) {
216 assert(getTargetTrue() == nullptr);
217 setDeleted();
218 return true;
219 }
220 // If there is no fallthrough node, such as a non-default case label for a
221 // switch instruction, then there is no opportunity to optimize.
222 if (getTargetTrue() == nullptr)
223 return false;
224 // If the fallthrough is to the next node, set fallthrough to nullptr to
225 // indicate.
226 if (getTargetTrue() == NextNode) {
227 TargetTrue = nullptr;
228 return true;
229 }
230 // If TargetFalse is the next node, and TargetTrue is not nullptr
231 // then invert the branch condition, swap the targets, and set new
232 // fallthrough to nullptr.
233 if (getTargetFalse() == NextNode) {
234 assert(Predicate != CondMIPS32::AL);
235 setPredicate(getOppositeCondition(getPredicate()));
236 TargetFalse = getTargetTrue();
237 TargetTrue = nullptr;
238 return true;
239 }
240 return false;
241 }
242
203 bool InstMIPS32Br::repointEdges(CfgNode *OldNode, CfgNode *NewNode) { 243 bool InstMIPS32Br::repointEdges(CfgNode *OldNode, CfgNode *NewNode) {
204 bool Found = false; 244 bool Found = false;
205 if (TargetFalse == OldNode) { 245 if (TargetFalse == OldNode) {
206 TargetFalse = NewNode; 246 TargetFalse = NewNode;
207 Found = true; 247 Found = true;
208 } 248 }
209 if (TargetTrue == OldNode) { 249 if (TargetTrue == OldNode) {
210 TargetTrue = NewNode; 250 TargetTrue = NewNode;
211 Found = true; 251 Found = true;
212 } 252 }
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 case CondMIPS32::LEZ: 452 case CondMIPS32::LEZ:
413 case CondMIPS32::LTZ: 453 case CondMIPS32::LTZ:
414 case CondMIPS32::GEZ: 454 case CondMIPS32::GEZ:
415 case CondMIPS32::GTZ: { 455 case CondMIPS32::GTZ: {
416 getSrc(0)->emit(Func); 456 getSrc(0)->emit(Func);
417 Str << ", "; 457 Str << ", ";
418 break; 458 break;
419 } 459 }
420 } 460 }
421 Str << getTargetFalse()->getAsmName(); 461 Str << getTargetFalse()->getAsmName();
462 if (getTargetTrue()) {
463 Str << "\n\t"
464 << "b"
465 << "\t" << getTargetTrue()->getAsmName();
466 }
422 } 467 }
423 } 468 }
424 } 469 }
425 470
426 void InstMIPS32Br::dump(const Cfg *Func) const { 471 void InstMIPS32Br::dump(const Cfg *Func) const {
427 if (!BuildDefs::dump()) 472 if (!BuildDefs::dump())
428 return; 473 return;
429 Ostream &Str = Func->getContext()->getStrDump(); 474 Ostream &Str = Func->getContext()->getStrDump();
430 Str << "\t" 475 Str << "\t"
431 "b" << InstMIPS32CondAttributes[Predicate].EmitString << "\t"; 476 "b" << InstMIPS32CondAttributes[Predicate].EmitString << "\t";
432 477
433 if (Label) { 478 if (Label) {
434 Str << Label->getLabelName(); 479 Str << Label->getLabelName();
435 } else { 480 } else {
436 if (isUnconditionalBranch()) { 481 if (isUnconditionalBranch()) {
437 Str << getTargetFalse()->getAsmName(); 482 Str << getTargetFalse()->getAsmName();
438 } else { 483 } else {
439 dumpSources(Func); 484 dumpSources(Func);
440 Str << ", "; 485 Str << ", ";
441 Str << getTargetFalse()->getAsmName(); 486 Str << getTargetFalse()->getAsmName();
487 if (getTargetTrue()) {
488 Str << "\n\t"
489 << "b"
490 << "\t" << getTargetTrue()->getAsmName();
491 }
442 } 492 }
443 } 493 }
444 } 494 }
445 495
446 void InstMIPS32Call::emit(const Cfg *Func) const { 496 void InstMIPS32Call::emit(const Cfg *Func) const {
447 if (!BuildDefs::dump()) 497 if (!BuildDefs::dump())
448 return; 498 return;
449 Ostream &Str = Func->getContext()->getStrEmit(); 499 Ostream &Str = Func->getContext()->getStrEmit();
450 assert(getSrcSize() == 1); 500 assert(getSrcSize() == 1);
451 if (llvm::isa<ConstantInteger32>(getCallTarget())) { 501 if (llvm::isa<ConstantInteger32>(getCallTarget())) {
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 getSrc(0)->emit(Func); 738 getSrc(0)->emit(Func);
689 return; 739 return;
690 } 740 }
691 741
692 // stack to stack 742 // stack to stack
693 llvm::report_fatal_error("mov cant copy stack to stack."); 743 llvm::report_fatal_error("mov cant copy stack to stack.");
694 } 744 }
695 745
696 } // end of namespace MIPS32 746 } // end of namespace MIPS32
697 } // end of namespace Ice 747 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceInstMIPS32.h ('k') | src/IceTargetLoweringMIPS32.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698