LLVM学习笔记(20)

本文详细介绍了DAGISelEmitter中的Match对象序列优化过程,包括Matcher对象的合并、谓词下沉以及公共因子提取等关键步骤。这些优化措施旨在减少匹配过程中不必要的检查,提高指令选择效率。
摘要由CSDN通过智能技术生成
3.4.3.3.       Match对象序列的优化

回到DAGISelEmitter::run。注意PatternMatchers中每个项都是一条Matcher对象链,对应一条指令。在160行生成一个ScopeMatcher对象,把这些项设为自己的孩子。在下面生成的MatcherTable里,它产生的内容将使得SelectCommon方法在匹配一个孩子(即一条指令)失败时,继续进行下一个孩子(即下一条指令)的匹配。

DAGISelEmitter::run(续)

160       std::unique_ptr<Matcher> TheMatcher =

161         llvm::make_unique<ScopeMatcher>(PatternMatchers);

162     

163        OptimizeMatcher(TheMatcher,CGP);

164       //Matcher->dump();

165        EmitMatcherTable(TheMatcher.get(),CGP, OS);

166      }

在输出MatcherTable之前,TableGen需要执行一个优化步骤。优化包括几方面。一方面在Matcher对象链里,可能存在若干Matcher对象序列,它们可以被更短的等效的Matcher对象序列所替代。另一方面,有些的Matcher对象可以作为公共因子提取出来,以形成更短的Matcher序列。

514      void

515      llvm::OptimizeMatcher(std::unique_ptr<Matcher>&MatcherPtr,

516                            constCodeGenDAGPatterns &CGP) {

517        ContractNodes(MatcherPtr,CGP);

518        SinkPatternPredicates(MatcherPtr);

519        FactorNodes(MatcherPtr);

520      }

3.4.3.3.1. 合并Matcher对象

在Matcher对象序列里,有些Matcher对象是可以合并的,比如一个MoveChildMatcher与一个RecordMatcher就可以合并为一个RecordChildMatcher。这样不仅缩短了Matcher对象的序列,而且用一个更具体的Matcher对象来代替两个Matcher对象也简化了后续对Matcher对象序列的处理。

26        static voidContractNodes(std::unique_ptr<Matcher>&MatcherPtr,

27                                 const CodeGenDAGPatterns &CGP) {

28          // If we reachedthe end of the chain, we're done.

29          Matcher *N = MatcherPtr.get();

30          if (!N) return;

31       

32          // If we have ascope node, walk down all of the children.

33          if (ScopeMatcher *Scope =dyn_cast<ScopeMatcher>(N)) {

34            for(unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {

35              std::unique_ptr<Matcher>Child(Scope->takeChild(i));

36              ContractNodes(Child, CGP);

37              Scope->resetChild(i, Child.release());

38            }

39            return;

40          }

41         

42          // If we found amovechild node with a node that comes in a 'foochild' form,

43         // transform it.

44          if (MoveChildMatcher *MC =dyn_cast<MoveChildMatcher>(N)) {

45            Matcher *New = nullptr;

46            if (RecordMatcher *RM =dyn_cast<RecordMatcher>(MC->getNext()))

47              if (MC->getChildNo() < 8) // Only have RecordChild0...7

48                New = newRecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),

49                                            RM->getResultNo());

50       

51            if (CheckTypeMatcher *CT =dyn_cast<CheckTypeMatcher>(MC->getNext()))

52              if (MC->getChildNo() < 8&& //Only have CheckChildType0...7

53                  CT->getResNo() == 0)    //CheckChildType checks res #0

54                New = newCheckChildTypeMatcher(MC->getChildNo(), CT->getType());

55       

56            if (CheckSameMatcher *CS =dyn_cast<CheckSameMatcher>(MC->getNext()))

57              if (MC->getChildNo() < 4) // Only haveCheckChildSame0...3

58                New = newCheckChildSameMatcher(MC->getChildNo(), CS->getMatchNumber());

59       

60            if (CheckIntegerMatcher *CS =dyn_cast<CheckIntegerMatcher>(MC->getNext()))

61              if (MC->getChildNo() < 5) // Only haveCheckChildInteger0...4

62                New = newCheckChildIntegerMatcher(MC->getChildNo(), CS->getValue());

63       

64            if (New) {

65              // Insert thenew node.

66              New->setNext(MatcherPtr.release());

67              MatcherPtr.reset(New);

68              // Remove theold one.

69             MC->setNext(MC->getNext()->takeNext());

70              returnContractNodes(MatcherPtr, CGP);

71            }

72          }

73         

74          // Zap movechild-> moveparent.

75          if (MoveChildMatcher *MC =dyn_cast<MoveChildMatcher>(N))

76            if (MoveParentMatcher *MP =

77                 dyn_cast<MoveParentMatcher>(MC->getNext())) {

78              MatcherPtr.reset(MP->takeNext());

79              returnContractNodes(MatcherPtr, CGP);

80            }

81       

82          // TurnEmitNode->MarkFlagResults->CompleteMatch into

83         //MarkFlagResults->EmitNode->CompleteMatch when we can to encourage

84         // MorphNodeToformation.  This is safe becauseMarkFlagResults never refers

85         // to the root ofthe pattern.

86          if (isa<EmitNodeMatcher>(N) &&isa<MarkGlueResultsMatcher>(N->getNext()) &&

87             isa<CompleteMatchMatcher>(N->getNext()->getNext())) {

88            // Unlink thetwo nodes from the list.

89            Matcher *EmitNode = MatcherPtr.release();

90            Matcher *MFR = EmitNode->takeNext();

91            Matcher *Tail = MFR->takeNext();

92               

93            // Relink them.

94            MatcherPtr.reset(MFR);

95            MFR->setNext(EmitNode);

96            EmitNode->setNext(Tail);

97            returnContractNodes(MatcherPtr, CGP);

98          }

99       

100        // TurnEmitNode->CompleteMatch into MorphNodeTo if we can.

101        if (EmitNodeMatcher *EN =dyn_cast<EmitNodeMatcher>(N))

102          if (CompleteMatchMatcher *CM =

103               dyn_cast<CompleteMatchMatcher>(EN->getNext())) {

104            // We canonly use MorphNodeTo if the result values match up.

105            unsigned RootResultFirst =EN->getFirstResultSlot();

106            bool ResultsMatch = true;

107            for(unsigned i = 0, e = CM->getNumResults(); i != e; ++i)

108              if (CM->getResult(i) !=RootResultFirst+i)

109                ResultsMatch = false;

110           

111            // If theselected node defines a subset of the glue/chain results, we

112           // can't useMorphNodeTo.  For example, we can't useMorphNodeTo if the

113           // matchedpattern has a chain but the root node doesn't.

114            constPatternToMatch &Pattern = CM->getPattern();

115           

116            if(!EN->hasChain() &&

117               Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))

118              ResultsMatch = false;

119     

120            // If thematched node has glue and the output root doesn't, we can't

121           // useMorphNodeTo.

122           //

123           // NOTE:Strictly speaking, we don't have to check for glue here

124           // becausethe code in the pattern generator doesn't handle it right.  We

125           // do itanyway for thoroughness.

126            if (!EN->hasOutFlag() &&

127               Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))

128              ResultsMatch = false;

129           

130           

131            // If theroot result node defines more results than the source root node

132           // *and* hasa chain or glue input, then we can't match it because it

133           // would endup replacing the extra result with the chain/glue.

134      #if0

135            if ((EN->hasGlue() ||EN->hasChain()) &&

136                EN->getNumNonChainGlueVTs() >... need to get no results reliably ...)

137              ResultMatch = false;

138      #endif

139               

140            if (ResultsMatch) {

141              constSmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();

142              constSmallVectorImpl<unsigned> &Operands = EN->getOperandList();

143              MatcherPtr.reset(newMorphNodeToMatcher(EN->getOpcodeName(),

144                                                     VTs, Operands,

145                                                     EN->hasChain(), EN->hasInFlag(),

146                                                     EN->hasOutFlag(),

147                                                     EN->hasMemRefs(),

148                                                     EN->getNumFixedArityOperands(),

149                                                      Pattern));

150              return;

151            }

152     

153            // FIXME2:Kill off all the SelectionDAG::SelectNodeTo and getMachineNode

154           // variants.

155          }

156       

157        ContractNodes(N->getNextPtr(), CGP);

158       

159       

160        // If we have aCheckType/CheckChildType/Record node followed by a

161       // CheckOpcode,invert the two nodes.  We prefer to dostructural checks

162       // before typechecks, as this opens opportunities for factoring on targets

163       // like X86 wheremany operations are valid on multiple types.

164        if ((isa<CheckTypeMatcher>(N) ||isa<CheckChildTypeMatcher>(N) ||

165             isa<RecordMatcher>(N)) &&

166           isa<CheckOpcodeMatcher>(N->getNext())) {

167          // Unlink thetwo nodes from the list.

168          Matcher *CheckType = MatcherPtr.release();

169          Matcher *CheckOpcode =CheckType->takeNext();

170          Matcher *Tail = CheckOpcode->takeNext();

171         

172          // Relink them.

173          MatcherPtr.reset(CheckOpcode);

174          CheckOpcode->setNext(CheckType);

175          CheckType->setNext(Tail);

176          returnContractNodes(MatcherPtr, CGP);

177        }

178      }

ContractNodes是一个递归函数。因为Matcher对象的序列以ScopeMatcher实开始,因此最上层的ContractNodes调用必然进入33~39行的分支。以后的递归调用则依据所见的Matcher实例分别进入不同的分支。有效的合并操作计有:

MoveChildMatcher + RecordMatcher à RecordChildMatcher(仅对第1~8的子节点)

MoveChildMatcher + CheckTypeMatcher à CheckChildTypeMatcher(仅对第1~8的子节点)

MoveChildMatcher + CheckSameMatcher à CheckChildSameMatcher(仅对第1~4的子节点)

MoveChildMatcher + CheckIntegerMatcher à CheckChildIntegerMatcher(仅对第1~5的子节点)

MoveChildMatcher + MoveParentMatcher à 0

EmitNodeMatcher + MarkGlueResultsMatcher+ CompleteMatchMatcher à

                    MarkGlueResultsMatcher + EmitNodeMatcher+ CompleteMatchMatcher

EmitNodeMatcher + CompleteMatchMatcher à MorphNodeToMatcher(在两者的结果都相同时)

CheckTypeMatcher | CheckChildTypeMatcher | RecordMatcher+ CheckOpcodeMatcher à

                  CheckOpcodeMatcher + CheckTypeMatcher | CheckChildTypeMatcher | RecordMatcher

3.4.3.3.2. 下调谓词

前面生成PatternToMatch实例时,模板谓词(Predicate)都被移到了模板的最上层。因此,在生成的Matcher实例里,CheckPredicateMatcher也是在最前面的,这会妨碍后面的因子提取看到它们。因此SinkPatternPredicates会尽量将这些CheckPredicateMatcher实例往下移,但不会越过需要判断谓词才执行的Matcher实例。

191      static voidSinkPatternPredicates(std::unique_ptr<Matcher>&MatcherPtr) {

192        // Recursivelyscan for a PatternPredicate.

193       // If we reachedthe end of the chain, we're done.

194        Matcher *N = MatcherPtr.get();

195        if (!N) return;

196       

197        // Walk down allmembers of a scope node.

198        if (ScopeMatcher *Scope =dyn_cast<ScopeMatcher>(N)) {

199          for(unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {

200            std::unique_ptr<Matcher>Child(Scope->takeChild(i));

201            SinkPatternPredicates(Child);

202            Scope->resetChild(i, Child.release());

203          }

204          return;

205        }

206       

207        // If this nodeisn't a CheckPatternPredicateMatcher we keep scanning until

208       // we find one.

209        CheckPatternPredicateMatcher *CPPM=dyn_cast<CheckPatternPredicateMatcher>(N);

210        if (!CPPM)

211          returnSinkPatternPredicates(N->getNextPtr());

212       

213        // Ok, we foundone, lets try to sink it. Check if we can sink it past the

214       // next node inthe chain.  If not, we won't be able tochange anything and

215       // might as wellbail.

216        if(!CPPM->getNext()->isSafeToReorderWithPatternPredicate())

217          return;

218       

219        // Okay, we knowwe can sink it past at least one node. Unlink it from the

220       // chain and scanfor the new insertion point.

221        MatcherPtr.release();  // Don't deleteCPPM.

222        MatcherPtr.reset(CPPM->takeNext());

223       

224        N = MatcherPtr.get();

225        while(N->getNext()->isSafeToReorderWithPatternPredicate())

226          N = N->getNext();

227       

228        // At this point,we want to insert CPPM after N.

229        CPPM->setNext(N->takeNext());

230        N->setNext(CPPM);

231      }

上面216与225行的isSafeToReorderWithPatternPredicate是虚函数,不同的Matcher派生定义依自己的需要进行改写。它如果返回true,表示可以将PatternPredicate下沉到它之下。

基类Matcher的isSafeToReorderWithPatternPredicate返回false。

而返回true的派生类有:RecordMatcher,RecordChildMatcher,RecordMemRefMatcher,CaptureGlueInputMatcher,MoveChildMatcher,MoveParentMatcher,CheckSameMatcher,CheckChildSameMatcher,CheckPatternPredicateMatcher,CheckOpcodeMatcher,CheckTypeMatcher,CheckChildTypeMatcher,CheckIntegerMatcher,CheckChildIntegerMatcher,CheckCondCodeMatcher,CheckValueTypeMatcher,CheckAndImmMatcher,CheckOrImmMatcher,CheckFoldableChainNodeMatcher。

返回false的派生类有:CheckComplexPatMatcher,CheckPredicateMatcher,ScopeMatcher,SwitchOpcodeMatcher,SwitchTypeMatcher,以及生成结果代码的Matcher对象(即EmitStringIntegerMatcher,EmitRegisterMatcher,EmitConvertToTargetMatcher,EmitMergeInputChainsMatcher,EmitCopyToRegMatcher,EmitNodeXFormMatcher,EmitNodeMatcher,MorphNodeToMatcher,MarkGlueResultsMatcher,CompleteMatchMatcher)。

3.4.3.3.3. 提取等价对象

完成了上述的优化后,FactorNodes将进行公共因子的提取,比如将如下的Matcher树:

   Scope +à OPC_CheckType i32 à ABC

               |

               +à OPC_CheckType i32 à XYZ

转换为:

   OPC_CheckType i32 à Scope +àABC

                                                         |

                                                         +àXYZ

ScopeMatcher是单一的Matcher节点,本身不指向下一个Matcher对象,但它有多个子节点,这些子节点都构成一条链(ScopeMatcher类型的子节点除外,它们是子树的根节点)。公共因子从这些子节点链上提取。

在263行,对非ScopeMatcher对象则不做处理,只是对它指向的下一个Matcher对象进行尾递归调用。因此到271行,当前节点就是一个ScopeMatcher对象,对其子节点进行递归调用。这是个深度优先的过程,保证在链最后的ScopeMatcher对象先得到处理,然后经由返回到递归调用点逐步向Matcher树中的前一个ScopeMatcher对象回溯。最后将处理后的子序列保存在OptionsToMatch中。

在OptimizeMatcher调用的FactorNodes时,Matcher序列只有一个ScopeMatcher对象,274行调用的FactorNodes实际上什么也不做,递归到序列尾返回。因此OptimizeMatcher调用的FactorNodes在271行的循环向容器OptionsToMatch插入了开头这个ScopeMatcher对象所有的子节点。

256      static voidFactorNodes(std::unique_ptr<Matcher>&MatcherPtr) {

257        // If we reachedthe end of the chain, we're done.

258        Matcher *N = MatcherPtr.get();

259        if (!N) return;

260       

261        // If this is nota push node, just scan for one.

262        ScopeMatcher *Scope =dyn_cast<ScopeMatcher>(N);

263        if (!Scope)

264          returnFactorNodes(N->getNextPtr());

265       

266        // Okay, pulltogether the children of the scope node into a vector so we can

267       // inspect itmore easily.  While we're at it, bucketthem up by the hash

268       // code of theirfirst predicate.

269        SmallVector<Matcher*, 32>OptionsToMatch;

270       

271        for (unsignedi = 0, e = Scope->getNumChildren(); i != e; ++i) {

272          // Factor thesubexpression.

273          std::unique_ptr<Matcher>Child(Scope->takeChild(i));

274          FactorNodes(Child);

275         

276          if (Matcher *N = Child.release())

277            OptionsToMatch.push_back(N);

278        }

279       

280        SmallVector<Matcher*, 32> NewOptionsToMatch;

281       

282        // Loop overoptions to match, merging neighboring patterns with identical

283        // starting nodesinto a shared matcher.

284        for (unsignedOptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {

285          // Find the setof matchers that start with this node.

286          Matcher *Optn =OptionsToMatch[OptionIdx++];

287     

288          if (OptionIdx == e) {

289            NewOptionsToMatch.push_back(Optn);

290            continue;

291          }

292         

293          // See if thenext option starts with the same matcher. If the two

294         // neighbors*do* start with the same matcher, we can factor the matcher out

295         // of at leastthese two patterns.  See what the maximalset we can merge

296         // together is.

297          SmallVector<Matcher*, 8> EqualMatchers;

298          EqualMatchers.push_back(Optn);

299         

300          // Factor allof the known-equal matchers after this one into the same

301         // group.

302          while(OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))

303           EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);

304

305          // If we founda non-equal matcher, see if it is contradictory with the

306         // currentnode.  If so, we know that the orderingrelation between the

307         // current setsof nodes and this node don't matter. Look past it to see if

308         // we can mergeanything else into this matching group.

309          unsigned Scan = OptionIdx;

310          while (1) {

311            // If we ranout of stuff to scan, we're done.

312            if (Scan == e) break;

313           

314            Matcher *ScanMatcher =OptionsToMatch[Scan];

315           

316            // If wefound an entry that matches out matcher, merge it into the set to

317           // handle.

318            if (Optn->isEqual(ScanMatcher)) {

319              // If is equalafter all, add the option to EqualMatchers and remove it

320             // fromOptionsToMatch.

321              EqualMatchers.push_back(ScanMatcher);

322             OptionsToMatch.erase(OptionsToMatch.begin()+Scan);

323              --e;

324              continue;

325           }

326           

327            // If theoption we're checking for contradicts the start of the list,

328           // skip overit.

329            if(Optn->isContradictory(ScanMatcher)) {

330              ++Scan;

331              continue;

332            }

333     

334            // If we'rescanning for a simple node, see if it occurs later in the

335           //sequence.  If so, and if we can move itup, it might be contradictory

336           // or thesame as what we're looking for.  If so,reorder it.

337            if (Optn->isSimplePredicateOrRecordNode()){

338              Matcher *M2 =FindNodeWithKind(ScanMatcher, Optn->getKind());

339              if (M2 && M2 != ScanMatcher&&

340                  M2->canMoveBefore(ScanMatcher)&&

341                  (M2->isEqual(Optn) ||M2->isContradictory(Optn))) {

342                Matcher *MatcherWithoutM2 =ScanMatcher->unlinkNode(M2);

343                M2->setNext(MatcherWithoutM2);

344                OptionsToMatch[Scan] = M2;

345                continue;

346              }

347            }

348           

349            // Otherwise,we don't know how to handle this entry, we have to bail.

350            break;

351          }

352           

353          if (Scan != e &&

354              // Don'tprint it's obvious nothing extra could be merged anyway.

355              Scan+1 != e) {

356            DEBUG(errs() << "Couldn'tmerge this:\n";

357                  Optn->print(errs(), 4);

358                  errs() << "intothis:\n";

359                 OptionsToMatch[Scan]->print(errs(), 4);

360                  if (Scan+1 != e)

361                   OptionsToMatch[Scan+1]->printOne(errs());

362                  if (Scan+2 < e)

363                   OptionsToMatch[Scan+2]->printOne(errs());

364                  errs() << "\n");

365          }

366         

367          // If we onlyfound one option starting with this matcher, no factoring is

368         // possible.

369          if (EqualMatchers.size() == 1) {

370           NewOptionsToMatch.push_back(EqualMatchers[0]);

371            continue;

372          }

373         

374          // Factor thesechecks by pulling the first node off each entry and

375         // discardingit.  Take the first one off the firstentry to reuse.

376          Matcher *Shared = Optn;

377          Optn = Optn->takeNext();

378          EqualMatchers[0] = Optn;

379     

380          // Remove anddelete the first node from the other matchers we're factoring.

381          for(unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {

382            Matcher *Tmp =EqualMatchers[i]->takeNext();

383            deleteEqualMatchers[i];

384            EqualMatchers[i] = Tmp;

385          }

386         

387          Shared->setNext(newScopeMatcher(EqualMatchers));

388     

389          // Recursivelyfactor the newly created node.

390          FactorNodes(Shared->getNextPtr());

391         

392          NewOptionsToMatch.push_back(Shared);

393        }

OptionsToMatch的每一项都代表一个等价类(但在一开始每一项代表一条指令,但在后面387与390行处理里,将逐步细化为等价类)。284行开始的循环执行等价类的合并。

首先在302行循环检查OptionsToMatch中紧邻的项是否等价,将等价类合并起来。而存在不等价情形时,310行循环确定这些对象是与当前等价类冲突或等价,或者满足337~341行条件的相邻Matcher序列。容器EqualMatchers用于保存与当前处理Matcher对象(Optn)等价的Matcher对象。

如果后续Matcher对象都不满足上述条件,它将属于另一个等价类。注意,因为生成Matcher时,PatternToMatch对象是经过排序的,因此等价Matcher对象通常会聚集在一起。等价意味着作用相同,因此在369行以下,如果EqualMatchers中有多个Matcher序列,可以将这些序列中的第一个元素提取出来,构造出一个ScopeMatcher对象,让FactorNodes递归处理后续的元素。最后将序列的处理结果记录在NewOptionsToMatch容器里,继续284行循环的处理。

用于判定等价关系的Matcher对象的isEqual方法是一个虚函数。其在基类Matcher的定义是:

105        bool isEqual(constMatcher *M) const {

106          if (getKind() != M->getKind())return false;

107          returnisEqualImpl(M);

108        }

不同类型间的Matcher对象总是不等价的。相同类型的Matcher对象由虚函数isEqualImpl来判别。其中ScopeMatcher、SwitchOpcodeMatcher、SwitchTypeMatcher不同实例间总是不等价;而RecordMemRefMatcher、CheckFoldableChainNodeMatcher、RecordMatcher、MoveParentMatcher、CaptureGlueInputMatcher不同实例间则总是等价的。

另外,等价的RecordChildMatcher对象必须具有相同的子节点编号(MoveChildMatcher类似):

292        boolisEqualImpl(const Matcher *M)const override {

293          returncast<RecordChildMatcher>(M)->getChildNo() == getChildNo();

294        }

等价的CheckSameMatcher对象必须具有相同的匹配值:

394        boolisEqualImpl(const Matcher *M)const override {

395          returncast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();

396        }

等价的CheckPatternPredicateMatcher对象则具有相同的名称谓词(CheckPredicateMatcher对象与之类似,但要求具有相同的谓词对象):

447        boolisEqualImpl(const Matcher *M)const override {

448          returncast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;

449        }

等价的CheckOpcodeMatcher对象则必须援引相同的SDNode节点(但不一定同一个)。

326      bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M)const{

327        // Note: pointerequality isn't enough here, we have to check the enum names

328        // to ensure thatthe nodes are for the same opcode.

329        returncast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==

330                Opcode.getEnumName();

331      }

等价的CheckTypeMatcher对象则要求援引相同的类型:

548        boolisEqualImpl(const Matcher *M)const override {

549          returncast<CheckTypeMatcher>(M)->Type == Type;

550        }

等价的CheckChildTypeMatcher对象则同时要求援引相同的类型及相同的子节点:

603        boolisEqualImpl(const Matcher *M)const override {

604          returncast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&

605                cast<CheckChildTypeMatcher>(M)->Type == Type;

606        }

等价CheckIntegerMatcher对象要求整数值相同(CheckAndImmMatcher、CheckOrImmMatcher有类似的要求):

630        boolisEqualImpl(const Matcher *M)const override {

631          returncast<CheckIntegerMatcher>(M)->Value == Value;

632        }

等价的CheckCondCodeMatcher对象则要求有相同的条件代码:

683        bool isEqualImpl(const Matcher *M)constoverride {

684          returncast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;

685        }

等价的CheckValueTypeMatcher对象要求援引相同名称的类型:

707        boolisEqualImpl(const Matcher *M)const override {

708          returncast<CheckValueTypeMatcher>(M)->TypeName == TypeName;

709        }

等价的CheckComplexPatMatcher对象要求具有相同的匹配节点编号:

752        boolisEqualImpl(const Matcher *M)const override {

753          return&cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern&&

754                cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;

755        }

等价的EmitIntegerMatcher对象要求值和类型都要相同(EmitStringIntegerMatcher有类似要求):

845      ol isEqualImpl(constMatcher *M)const override{

846          returncast<EmitIntegerMatcher>(M)->Val == Val &&

847                cast<EmitIntegerMatcher>(M)->VT == VT;

848        }

等价的EmitRegisterMatcher对象则要求相同的寄存器对象及类型:

896        boolisEqualImpl(const Matcher *M)const override {

897          returncast<EmitRegisterMatcher>(M)->Reg == Reg &&

898                cast<EmitRegisterMatcher>(M)->VT == VT;

899        }

等价的EmitConvertToTargetMatcher要求具有援引相同的对象:

922        boolisEqualImpl(const Matcher *M)const override {

923          returncast<EmitConvertToTargetMatcher>(M)->Slot == Slot;

924        }

等价的EmitMergeInputChainsMatcher对象要求具有相同的输入链:

951        boolisEqualImpl(const Matcher *M)const override {

952          returncast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;

953        }

等价的EmitCopyToRegMatcher对象要求援引相同的源数据与目标寄存器:

976        boolisEqualImpl(const Matcher *M)const override {

977          returncast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&

978                cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;

979        }

等价的EmitNodeXFormMatcher对象要求援引相同的对象及转换方法:

1005     boolisEqualImpl(const Matcher *M)const override {

1006       returncast<EmitNodeXFormMatcher>(M)->Slot == Slot &&

1007             cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;

1008    }

等价的EmitNodeMatcherCommon对象要在各方面完全一致:

333      bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m)const{

334        constEmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);

335        returnM->OpcodeName == OpcodeName && M->VTs == VTs &&

336               M->Operands == Operands &&M->HasChain == HasChain &&

337               M->HasInGlue == HasInGlue&& M->HasOutGlue == HasOutGlue &&

338               M->HasMemRefs == HasMemRefs&&

339               M->NumFixedArityOperands ==NumFixedArityOperands;

340      }

等价的MarkGlueResultsMatcher对象要求写相同的标记寄存器:

1141     boolisEqualImpl(const Matcher *M)const override {

1142       returncast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;

1143    }

等价的CompleteMatchMatcher对象要求有相同的结果与模式:

1169     boolisEqualImpl(const Matcher *M)const override {

1170       returncast<CompleteMatchMatcher>(M)->Results == Results &&

1171            &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;

1172    }

类似的,相违关系则是由另一个虚函数isContradictory来判定。如果两个Matcher对象不可能匹配到同一个DAG,该方法会返回true。反过来,如果返回false,只是表明不确定是否能匹配到同一个DAG。

168        boolisContradictory(const Matcher *Other)const {

169          // Since thispredicate is reflexive, we canonicalize the ordering so that

170         // we alwaysmatch a node against nodes with kinds that are greater or equal

171         // tothem.  For example, we'll pass in aCheckType node as an argument to

172         // the CheckOpcodemethod, not the other way around.

173          if (getKind() < Other->getKind())

174            returnisContradictoryImpl(Other);

175          returnOther->isContradictoryImpl(this);

176        }

同样,isContradictoryImpl是由各个派生类定义的虚函数,基类Matcher提供的版本则总是返回false。定义了isContradictoryImpl的派生类有这些:

410      bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M)const{

411        if (constCheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {

412          // If the twochecks are about different nodes, we don't know if they

413         // conflict!

414          if (CC->getChildNo() != getChildNo())

415            returnfalse;

416     

417          return TypesAreContradictory(getType(),CC->getType());

418        }

419        return false;

420      }

如果这两个CheckChildTypeMatcher对象援引不同的节点,就无法确定这些对象是否不能匹配到同一个节点,因此返回false;否则通过TypesAreContradictory方法继续检查类型相容性。

362      static boolTypesAreContradictory(MVT::SimpleValueType T1,

363                                       MVT::SimpleValueType T2) {

364        // If the twotypes are the same, then they are the same, so they don't

365       // contradict.

366        if (T1 == T2) returnfalse;

367     

368        // If either typeis about iPtr, then they don't conflict unless the other

369       // one is not ascalar integer type.

370        if (T1 == MVT::iPTR)

371          return!MVT(T2).isInteger() || MVT(T2).isVector();

372     

373        if (T2 == MVT::iPTR)

374          return!MVT(T1).isInteger() || MVT(T1).isVector();

375     

376        // Otherwise,they are two different non-iPTR types, they conflict.

377        return true;

378      }

如果类型不相兼容,就可断定不能匹配到同一个节点。

对CheckOpcodeMatcher而言,如果操作码不同,两个CheckOpcodeMatcher就不能匹配到同一节点(正好是isEqualImpl的相反条件)。另外,它也可能与一个CheckTypeMatcher不起冲突。前提是这两个Matcher实例的类型是兼容的。

380      bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M)const{

381        if (const CheckOpcodeMatcher *COM =dyn_cast<CheckOpcodeMatcher>(M)) {

382          // One nodecan't have two different opcodes!

383         // Note:pointer equality isn't enough here, we have to check the enum names

384         // to ensurethat the nodes are for the same opcode.

385          returnCOM->getOpcode().getEnumName() != getOpcode().getEnumName();

386        }

387     

388        // If the nodehas a known type, and if the type we're checking for is

389       // different,then we know they contradict.  Forexample, a check for

390        // ISD::STOREwill never be true at the same time a check for Type i32 is.

391        if (constCheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {

392          // If checkingfor a result the opcode doesn't have, it can't match.

393          if (CT->getResNo() >=getOpcode().getNumResults())

394            returntrue;

395     

396          MVT::SimpleValueType NodeType =getOpcode().getKnownType(CT->getResNo());

397          if (NodeType != MVT::Other)

398           returnTypesAreContradictory(NodeType,CT->getType());

399        }

400     

401        return false;

402      }

兼容的CheckOpcodeMatcher与CheckTypeMatcher正好给出了进入FactorNode337行的一个例子。在337行的isSimplePredicateOrRecordNode方法检查下列条件:

147        bool isSimplePredicateOrRecordNode() const {

148          return isSimplePredicateNode() ||

149                 getKind() == RecordNode || getKind()== RecordChild;

150        }

满足isSimplePredicateNode方法的Matcher对象有:

124        bool isSimplePredicateNode() const{

125          switch(getKind()) {

126          default: return false;

127          caseCheckSame:

128          caseCheckChildSame:

129          caseCheckPatternPredicate:

130          caseCheckPredicate:

131          caseCheckOpcode:

132          caseCheckType:

133          caseCheckChildType:

134          caseCheckInteger:

135          caseCheckChildInteger:

136          caseCheckCondCode:

137          caseCheckValueType:

138          caseCheckAndImm:

139          caseCheckOrImm:

140          caseCheckFoldableChainNode:

141            returntrue;

142          }

143        }

CheckOpcodeMatcher与CheckTypeMatcher都满足isSimplePredicateNode方法。接下来338行的FindNodeWithKind查找ScanMatcher后续Matcher对象中第一个与Optn类型相同的对象,如果这个对象可以移到ScanMatcher前面,就交换它们(M2存在与Optn等价的可能性),并由310行循环重新处理。

55        bool Matcher::canMoveBefore(const Matcher *Other) const{

56          for (;; Other= Other->getNext()) {

57            assert(Other&& "Other didn't come before 'this'?");

58            if (this ==Other)return true;

59       

60            // We have tobe able to move this node across the Other node.

61            if (!canMoveBeforeNode(Other))

62              returnfalse;

63          }

64        }

在上面的方法中,this必须在Other后面。而只有满足下列条件,this才能移到Other前。

68        bool Matcher::canMoveBeforeNode(const Matcher *Other)const{

69          // We can movesimple predicates before record nodes.

70          if (isSimplePredicateNode())

71            returnOther->isSimplePredicateOrRecordNode();

72       

73          // We can moverecord nodes across simple predicates.

74          if (isSimplePredicateOrRecordNode())

75            returnisSimplePredicateNode();

76       

77          // We can't moverecord nodes across each other etc.

78          return false;

79        }

不过反过来CheckTypeMatcher的isContradictoryImpl并没有考虑CheckOpcodeMatcher,这是因为CheckTypeMatcher的类型标识值要小于CheckOpcodeMatcher,Matcher::isContradictory保证不会对CheckOpcodeMatcher调用CheckTypeMatcher的isContradictoryImpl方法。

404      bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M)const{

405        if (constCheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))

406          returnTypesAreContradictory(getType(), CT->getType());

407        return false;

408      }

至于CheckIntegerMatcher对象,只要援引的值不一样,就是不兼容的(与isEqualImpl相反)。

422      bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M)const{

423        if (constCheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))

424          returnCIM->getValue() != getValue();

425        return false;

426      }

同样对于CheckValueTypeMatcher对象,只要援引的类型的名称不同,就不可能兼容(与isEqualImpl相反)。

440      bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M)const{

441        if (constCheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))

442          returnCVT->getTypeName() != getTypeName();

443        return false;

444      }

回到FactorNodes。现在处理后的Matcher序列保存在容器NewOptionsToMatch中。接着,415行的分支检查指定的Matcher链是否以CheckOpcodeMatcher开始。427行分支接着检查该Matcher链中最靠近的CheckTypeMatcher对象是否能移到Matcher序列的前面。如果对所有的Matcher子序列,4都满足415行分支,就可以使用一个SwitchOpcodeMatcher对象封装它们。而如果是都满足427行分支,则可以把所有这些CheckTypeMatcher实例以一个SwitchTypeMatcher对象或CheckTypeMatcher对象代替(只存在CheckTypeMatcher多个相同的实例)。

在467行分支,容器TypeEntry用来记录这些CheckTypeMatcher对象所进行的检查类型,如果不存在相同的CheckTypeMatcher对象,在Cases容器里记录检查类型与去掉这个CheckTypeMatcher对象后的Matcher链(494行)。如果存在相同的CheckTypeMatcher对象,479行条件满足。如果前一个相同的CheckTypeMatcher对象所在链的当前对象不是ScopeMatcher,创建一个ScopeMatcher对象,以前一个Matcher链及当前Matcher链(都去掉相关CheckTypeMatcher对象)为孩子。而如果前一个相同的CheckTypeMatcher对象所在链的当前对象是ScopeMatcher,它必定是前一个情形创建的,只需把当前的Matcher链添加为这个ScopeMatcher对象的新孩子即可。进而在501行,一个新的等效的CheckTypeMatcher对象被添加到这个ScopeMatcher对象的前面。

FactorNodes(续)

395        // If we're down to a single pattern to match, then wedon't need this scope

396       // anymore.

397        if (NewOptionsToMatch.size() == 1) {

398          MatcherPtr.reset(NewOptionsToMatch[0]);

399          return;

400        }

401       

402        if (NewOptionsToMatch.empty()) {

403          MatcherPtr.reset();

404          return;

405        }

406       

407        // If ourfactoring failed (didn't achieve anything) see if we can simplify in

408       // other ways.

409       

410       // Check to seeif all of the leading entries are now opcode checks.  If so,

411       // we can convertthis Scope to be a OpcodeSwitch instead.

412        bool AllOpcodeChecks = true, AllTypeChecks =true;

413        for (unsignedi = 0, e = NewOptionsToMatch.size(); i != e; ++i) {

414          // Check to seeif this breaks a series of CheckOpcodeMatchers.

415          if (AllOpcodeChecks &&

416             !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {

417      #if0

418            if (i > 3) {

419              errs() << "FAILING OPC#" << i << "\n";

420              NewOptionsToMatch[i]->dump();

421            }

422      #endif

423            AllOpcodeChecks = false;

424          }

425     

426          // Check to seeif this breaks a series of CheckTypeMatcher's.

427          if (AllTypeChecks) {

428            CheckTypeMatcher *CTM =

429             cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],

430                                                             Matcher::CheckType));

431            if (!CTM ||

432                // iPTRchecks could alias any other case without us knowing, don't

433               // botherwith them.

434                CTM->getType() == MVT::iPTR ||

435                //SwitchType only works for result #0.

436                CTM->getResNo() != 0 ||

437                // If theCheckType isn't at the start of the list, see if we can move

438               // itthere.

439               !CTM->canMoveBefore(NewOptionsToMatch[i])) {

440      #if0

441              if (i > 3 && AllTypeChecks){

442                errs() << "FAILING TYPE#" << i << "\n";

443                NewOptionsToMatch[i]->dump();

444              }

445      #endif

446              AllTypeChecks = false;

447            }

448          }

449        }

450       

451        // If all theoptions are CheckOpcode's, we can form the SwitchOpcode, woot.

452        if (AllOpcodeChecks) {

453          StringSet<> Opcodes;

454          SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;

455          for(unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {

456            CheckOpcodeMatcher *COM =cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);

457            assert(Opcodes.insert(COM->getOpcode().getEnumName()).second&&

458                   "Duplicate opcodes notfactored?");

459           Cases.push_back(std::make_pair(&COM->getOpcode(),COM->getNext()));

460          }

461         

462          MatcherPtr.reset(newSwitchOpcodeMatcher(Cases));

463          return;

464        }

465       

466        // If all theoptions are CheckType's, we can form the SwitchType, woot.

467        if (AllTypeChecks) {

468          DenseMap<unsigned, unsigned>TypeEntry;

469         SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8>Cases;

470          for(unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {

471            CheckTypeMatcher *CTM =

472             cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],

473                                                             Matcher::CheckType));

474            Matcher *MatcherWithoutCTM =NewOptionsToMatch[i]->unlinkNode(CTM);

475            MVT::SimpleValueType CTMTy =CTM->getType();

476            deleteCTM;

477           

478            unsigned &Entry = TypeEntry[CTMTy];

479            if (Entry != 0) {

480              // If wehave unfactored duplicate types, then we should factor them.

481              Matcher *PrevMatcher =Cases[Entry-1].second;

482              if (ScopeMatcher *SM =dyn_cast<ScopeMatcher>(PrevMatcher)) {

483               SM->setNumChildren(SM->getNumChildren()+1);

484               SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);

485                continue;

486              }

487             

488              Matcher *Entries[2] = { PrevMatcher,MatcherWithoutCTM };

489              Cases[Entry-1].second =new ScopeMatcher(Entries);

490              continue;

491            }

492           

493            Entry = Cases.size()+1;

494            Cases.push_back(std::make_pair(CTMTy,MatcherWithoutCTM));

495          }

496         

497          if (Cases.size() != 1) {

498            MatcherPtr.reset(newSwitchTypeMatcher(Cases));

499          } else {

500            // If wefactored and ended up with one case, create it now.

501            MatcherPtr.reset(newCheckTypeMatcher(Cases[0].first, 0));

502            MatcherPtr->setNext(Cases[0].second);

503          }

504          return;

505        }

506       

507     

508        // Reassemble theScope node with the adjusted children.

509       Scope->setNumChildren(NewOptionsToMatch.size());

510        for (unsignedi = 0, e = NewOptionsToMatch.size(); i != e; ++i)

511          Scope->resetChild(i,NewOptionsToMatch[i]);

512      }

如果上述两种情形都不存在,那么只能将NewOptionsToMatch(即新的等价对象)替换为原来ScopeMatcher对象的孩子(509行以下)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值