• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MAPLE_IR_INCLUDE_MIR_FUNCTION_H
17 #define MAPLE_IR_INCLUDE_MIR_FUNCTION_H
18 #include <string>
19 #include "mir_module.h"
20 #include "mir_const.h"
21 #include "mir_symbol.h"
22 #include "mir_preg.h"
23 #include "intrinsics.h"
24 #include "file_layout.h"
25 #include "mir_nodes.h"
26 #include "mir_type.h"
27 #include "mir_scope.h"
28 #include "profile.h"
29 #include "func_desc.h"
30 
31 #define DEBUGME true
32 
33 namespace maple {
34 enum PointerAttr : uint32_t { kPointerUndeiced = 0x1, kPointerNull = 0x2, kPointerNoNull = 0x3 };
35 
36 enum FuncAttrProp : uint32_t {
37     kNoThrowException = 0x1,
38     kNoRetNewlyAllocObj = 0x2,
39     kNoDefEffect = 0x4,
40     kNoDefArgEffect = 0x8,
41     kPureFunc = 0x10,
42     kIpaSeen = 0x20,
43     kUseEffect = 0x40,
44     kDefEffect = 0x80
45 };
46 
47 // describe a formal definition in a function declaration
48 class FormalDef {
49 public:
50     GStrIdx formalStrIdx = GStrIdx(0);  // used when processing the prototype
51     MIRSymbol *formalSym = nullptr;     // used in the function definition
52     TyIdx formalTyIdx = TyIdx();
53     TypeAttrs formalAttrs = TypeAttrs();  // the formal's type attributes
54 
FormalDef()55     FormalDef() {};
~FormalDef()56     virtual ~FormalDef() {}
FormalDef(MIRSymbol * s,const TyIdx & tidx,const TypeAttrs & at)57     FormalDef(MIRSymbol *s, const TyIdx &tidx, const TypeAttrs &at) : formalSym(s), formalTyIdx(tidx), formalAttrs(at)
58     {
59     }
FormalDef(const GStrIdx & sidx,MIRSymbol * s,const TyIdx & tidx,const TypeAttrs & at)60     FormalDef(const GStrIdx &sidx, MIRSymbol *s, const TyIdx &tidx, const TypeAttrs &at)
61         : formalStrIdx(sidx), formalSym(s), formalTyIdx(tidx), formalAttrs(at)
62     {
63     }
64 };
65 
66 class MeFunction;         // circular dependency exists, no other choice
67 class EAConnectionGraph;  // circular dependency exists, no other choice
68 class MemReferenceTable;
69 class MIRFunction {
70 public:
MIRFunction(MIRModule * mod,StIdx idx)71     MIRFunction(MIRModule *mod, StIdx idx) : module(mod), symbolTableIdx(idx)
72     {
73         scope = module->GetMemPool()->New<MIRScope>(mod);
74     }
75 
76     ~MIRFunction() = default;
77 
78     void Dump(bool withoutBody = false);
79     void DumpUpFormal(int32 indent) const;
80     void DumpFrame(int32 indent) const;
81     void DumpFuncBody(int32 indent);
82     void DumpScope();
83     const MIRSymbol *GetFuncSymbol() const;
84     MIRSymbol *GetFuncSymbol();
85 
86     void SetBaseClassFuncNames(GStrIdx strIdx);
SetMemPool(MemPool * memPool)87     void SetMemPool(MemPool *memPool)
88     {
89         SetCodeMemPool(memPool);
90         codeMemPoolAllocator.SetMemPool(codeMemPool);
91     }
92 
93     /// update signature_strIdx, basefunc_strIdx, baseclass_strIdx, basefunc_withtype_strIdx
94     /// without considering baseclass_strIdx, basefunc_strIdx's original non-zero values
95     /// \param strIdx full_name strIdx of the new function name
96     void OverrideBaseClassFuncNames(GStrIdx strIdx);
97     const std::string &GetName() const;
98 
99     GStrIdx GetNameStrIdx() const;
100 
101     const std::string &GetBaseClassName() const;
102 
103     const std::string &GetBaseFuncName() const;
104 
105     const std::string &GetBaseFuncNameWithType() const;
106 
107     const std::string &GetBaseFuncSig() const;
108 
109     const std::string &GetSignature() const;
110 
GetBaseClassNameStrIdx()111     GStrIdx GetBaseClassNameStrIdx() const
112     {
113         return baseClassStrIdx;
114     }
115 
GetBaseFuncNameStrIdx()116     GStrIdx GetBaseFuncNameStrIdx() const
117     {
118         return baseFuncStrIdx;
119     }
120 
GetBaseFuncNameWithTypeStrIdx()121     GStrIdx GetBaseFuncNameWithTypeStrIdx() const
122     {
123         return baseFuncWithTypeStrIdx;
124     }
125 
GetBaseFuncSigStrIdx()126     GStrIdx GetBaseFuncSigStrIdx() const
127     {
128         return baseFuncSigStrIdx;
129     }
130 
SetBaseClassNameStrIdx(GStrIdx id)131     void SetBaseClassNameStrIdx(GStrIdx id)
132     {
133         baseClassStrIdx = id;
134     }
135 
SetBaseFuncNameStrIdx(GStrIdx id)136     void SetBaseFuncNameStrIdx(GStrIdx id)
137     {
138         baseFuncStrIdx = id;
139     }
140 
SetBaseFuncNameWithTypeStrIdx(GStrIdx id)141     void SetBaseFuncNameWithTypeStrIdx(GStrIdx id)
142     {
143         baseFuncWithTypeStrIdx = id;
144     }
145 
146     const MIRType *GetReturnType() const;
147     MIRType *GetReturnType();
IsReturnVoid()148     bool IsReturnVoid() const
149     {
150         return GetReturnType()->GetPrimType() == PTY_void;
151     }
GetReturnTyIdx()152     TyIdx GetReturnTyIdx() const
153     {
154         CHECK_FATAL(funcType != nullptr, "funcType is nullptr");
155         return funcType->GetRetTyIdx();
156     }
SetReturnTyIdx(TyIdx tyidx)157     void SetReturnTyIdx(TyIdx tyidx)
158     {
159         CHECK_FATAL(funcType != nullptr, "funcType is nullptr");
160         funcType->SetRetTyIdx(tyidx);
161     }
162 
163     const MIRType *GetClassType() const;
GetClassTyIdx()164     TyIdx GetClassTyIdx() const
165     {
166         return classTyIdx;
167     }
SetClassTyIdx(TyIdx tyIdx)168     void SetClassTyIdx(TyIdx tyIdx)
169     {
170         classTyIdx = tyIdx;
171     }
SetClassTyIdx(uint32 idx)172     void SetClassTyIdx(uint32 idx)
173     {
174         classTyIdx.reset(idx);
175     }
176 
AddArgument(MIRSymbol * st)177     void AddArgument(MIRSymbol *st)
178     {
179         DEBUG_ASSERT(st != nullptr, "null ptr check");
180         FormalDef formalDef(st->GetNameStrIdx(), st, st->GetTyIdx(), st->GetAttrs());
181         formalDefVec.push_back(formalDef);
182     }
183 
AddFormalDef(const FormalDef & formalDef)184     void AddFormalDef(const FormalDef &formalDef)
185     {
186         formalDefVec.push_back(formalDef);
187     }
188 
GetParamSize()189     size_t GetParamSize() const
190     {
191         CHECK_FATAL(funcType != nullptr, "funcType is nullptr");
192         return funcType->GetParamTypeList().size();
193     }
194 
GetParamTypes()195     auto &GetParamTypes() const
196     {
197         CHECK_FATAL(funcType != nullptr, "funcType is nullptr");
198         return funcType->GetParamTypeList();
199     }
200 
GetNthParamTyIdx(size_t i)201     TyIdx GetNthParamTyIdx(size_t i) const
202     {
203         DEBUG_ASSERT(i < funcType->GetParamTypeList().size(), "array index out of range");
204         return funcType->GetParamTypeList()[i];
205     }
206 
207     const MIRType *GetNthParamType(size_t i) const;
208     MIRType *GetNthParamType(size_t i);
209 
GetNthParamAttr(size_t i)210     const TypeAttrs &GetNthParamAttr(size_t i) const
211     {
212         DEBUG_ASSERT(i < formalDefVec.size(), "array index out of range");
213         DEBUG_ASSERT(formalDefVec[i].formalSym != nullptr, "null ptr check");
214         return formalDefVec[i].formalSym->GetAttrs();
215     }
216 
217     void UpdateFuncTypeAndFormals(const std::vector<MIRSymbol *> &symbols, bool clearOldArgs = false);
218     void UpdateFuncTypeAndFormalsAndReturnType(const std::vector<MIRSymbol *> &symbols, const TyIdx &retTyIdx,
219                                                bool clearOldArgs = false);
220     LabelIdx GetOrCreateLableIdxFromName(const std::string &name);
GetLabelStringIndex(LabelIdx labelIdx)221     GStrIdx GetLabelStringIndex(LabelIdx labelIdx) const
222     {
223         CHECK_FATAL(labelTab != nullptr, "labelTab is nullptr");
224         DEBUG_ASSERT(labelIdx < labelTab->Size(), "index out of range in GetLabelStringIndex");
225         return labelTab->GetSymbolFromStIdx(labelIdx);
226     }
GetLabelName(LabelIdx labelIdx)227     const std::string &GetLabelName(LabelIdx labelIdx) const
228     {
229         GStrIdx strIdx = GetLabelStringIndex(labelIdx);
230         return GlobalTables::GetStrTable().GetStringFromStrIdx(strIdx);
231     }
232 
233     const MIRSymbol *GetLocalOrGlobalSymbol(const StIdx &idx, bool checkFirst = false) const;
234     MIRSymbol *GetLocalOrGlobalSymbol(const StIdx &idx, bool checkFirst = false);
235 
236     void SetAttrsFromSe(uint8 specialEffect);
237 
GetAttrs()238     const FuncAttrs &GetAttrs() const
239     {
240         return funcAttrs;
241     }
242 
SetAttrs(FuncAttrs attr)243     void SetAttrs(FuncAttrs attr)
244     {
245         funcAttrs = attr;
246     }
247 
GetAttr(FuncAttrKind attrKind)248     bool GetAttr(FuncAttrKind attrKind) const
249     {
250         return funcAttrs.GetAttr(attrKind);
251     }
252 
SetAttr(FuncAttrKind attrKind)253     void SetAttr(FuncAttrKind attrKind)
254     {
255         funcAttrs.SetAttr(attrKind);
256     }
257 
UnSetAttr(FuncAttrKind attrKind)258     void UnSetAttr(FuncAttrKind attrKind)
259     {
260         funcAttrs.SetAttr(attrKind, true);
261     }
262 
IsVarargs()263     bool IsVarargs() const
264     {
265         return funcAttrs.GetAttr(FUNCATTR_varargs);
266     }
267 
IsWeak()268     bool IsWeak() const
269     {
270         return funcAttrs.GetAttr(FUNCATTR_weak);
271     }
272 
IsStatic()273     bool IsStatic() const
274     {
275         return funcAttrs.GetAttr(FUNCATTR_static);
276     }
277 
IsInline()278     bool IsInline() const
279     {
280         return funcAttrs.GetAttr(FUNCATTR_inline);
281     }
282 
IsExtern()283     bool IsExtern() const
284     {
285         return funcAttrs.GetAttr(FUNCATTR_extern);
286     }
287 
IsNative()288     bool IsNative() const
289     {
290         return funcAttrs.GetAttr(FUNCATTR_native);
291     }
292 
IsFinal()293     bool IsFinal() const
294     {
295         return funcAttrs.GetAttr(FUNCATTR_final);
296     }
297 
IsAbstract()298     bool IsAbstract() const
299     {
300         return funcAttrs.GetAttr(FUNCATTR_abstract);
301     }
302 
IsPublic()303     bool IsPublic() const
304     {
305         return funcAttrs.GetAttr(FUNCATTR_public);
306     }
307 
IsPrivate()308     bool IsPrivate() const
309     {
310         return funcAttrs.GetAttr(FUNCATTR_private);
311     }
312 
IsProtected()313     bool IsProtected() const
314     {
315         return funcAttrs.GetAttr(FUNCATTR_protected);
316     }
317 
IsConstructor()318     bool IsConstructor() const
319     {
320         return funcAttrs.GetAttr(FUNCATTR_constructor);
321     }
322 
IsLocal()323     bool IsLocal() const
324     {
325         return funcAttrs.GetAttr(FUNCATTR_local);
326     }
327 
IsNoDefArgEffect()328     bool IsNoDefArgEffect() const
329     {
330         return funcAttrs.GetAttr(FUNCATTR_nodefargeffect);
331     }
332 
IsNoDefEffect()333     bool IsNoDefEffect() const
334     {
335         return funcAttrs.GetAttr(FUNCATTR_nodefeffect);
336     }
337 
IsNoRetGlobal()338     bool IsNoRetGlobal() const
339     {
340         return funcAttrs.GetAttr(FUNCATTR_noretglobal);
341     }
342 
IsNoThrowException()343     bool IsNoThrowException() const
344     {
345         return funcAttrs.GetAttr(FUNCATTR_nothrow_exception);
346     }
347 
IsNoRetArg()348     bool IsNoRetArg() const
349     {
350         return funcAttrs.GetAttr(FUNCATTR_noretarg);
351     }
352 
IsNoPrivateDefEffect()353     bool IsNoPrivateDefEffect() const
354     {
355         return funcAttrs.GetAttr(FUNCATTR_noprivate_defeffect);
356     }
357 
IsIpaSeen()358     bool IsIpaSeen() const
359     {
360         return funcAttrs.GetAttr(FUNCATTR_ipaseen);
361     }
362 
IsPure()363     bool IsPure() const
364     {
365         return funcAttrs.GetAttr(FUNCATTR_pure);
366     }
367 
IsFirstArgReturn()368     bool IsFirstArgReturn() const
369     {
370         return funcAttrs.GetAttr(FUNCATTR_firstarg_return);
371     }
372 
IsUnSafe()373     bool IsUnSafe() const
374     {
375         return !funcAttrs.GetAttr(FUNCATTR_safed) || funcAttrs.GetAttr(FUNCATTR_unsafed);
376     }
377 
IsSafe()378     bool IsSafe() const
379     {
380         return funcAttrs.GetAttr(FUNCATTR_safed);
381     }
382 
SetVarArgs()383     void SetVarArgs()
384     {
385         funcAttrs.SetAttr(FUNCATTR_varargs);
386     }
387 
SetNoDefArgEffect()388     void SetNoDefArgEffect()
389     {
390         funcAttrs.SetAttr(FUNCATTR_nodefargeffect);
391     }
392 
SetNoDefEffect()393     void SetNoDefEffect()
394     {
395         funcAttrs.SetAttr(FUNCATTR_nodefeffect);
396     }
397 
SetNoRetGlobal()398     void SetNoRetGlobal()
399     {
400         funcAttrs.SetAttr(FUNCATTR_noretglobal);
401     }
402 
SetNoThrowException()403     void SetNoThrowException()
404     {
405         funcAttrs.SetAttr(FUNCATTR_nothrow_exception);
406     }
407 
SetNoRetArg()408     void SetNoRetArg()
409     {
410         funcAttrs.SetAttr(FUNCATTR_noretarg);
411     }
412 
SetNoPrivateDefEffect()413     void SetNoPrivateDefEffect()
414     {
415         funcAttrs.SetAttr(FUNCATTR_noprivate_defeffect);
416     }
417 
SetIpaSeen()418     void SetIpaSeen()
419     {
420         funcAttrs.SetAttr(FUNCATTR_ipaseen);
421     }
422 
SetPure()423     void SetPure()
424     {
425         funcAttrs.SetAttr(FUNCATTR_pure);
426     }
427 
SetFirstArgReturn()428     void SetFirstArgReturn()
429     {
430         funcAttrs.SetAttr(FUNCATTR_firstarg_return);
431     }
432 
UnsetNoDefArgEffect()433     void UnsetNoDefArgEffect()
434     {
435         funcAttrs.SetAttr(FUNCATTR_nodefargeffect, true);
436     }
437 
UnsetNoDefEffect()438     void UnsetNoDefEffect()
439     {
440         funcAttrs.SetAttr(FUNCATTR_nodefeffect, true);
441     }
442 
UnsetNoRetGlobal()443     void UnsetNoRetGlobal()
444     {
445         funcAttrs.SetAttr(FUNCATTR_noretglobal, true);
446     }
447 
UnsetNoThrowException()448     void UnsetNoThrowException()
449     {
450         funcAttrs.SetAttr(FUNCATTR_nothrow_exception, true);
451     }
452 
UnsetPure()453     void UnsetPure()
454     {
455         funcAttrs.SetAttr(FUNCATTR_pure, true);
456     }
457 
UnsetNoRetArg()458     void UnsetNoRetArg()
459     {
460         funcAttrs.SetAttr(FUNCATTR_noretarg, true);
461     }
462 
UnsetNoPrivateDefEffect()463     void UnsetNoPrivateDefEffect()
464     {
465         funcAttrs.SetAttr(FUNCATTR_noprivate_defeffect, true);
466     }
467 
468     bool HasCall() const;
469     void SetHasCall();
470 
471     bool IsReturnStruct() const;
472     void SetReturnStruct();
473     void SetReturnStruct(const MIRType &retType);
474 
475     bool IsUserFunc() const;
476     void SetUserFunc();
477 
478     bool IsInfoPrinted() const;
479     void SetInfoPrinted();
480     void ResetInfoPrinted();
481 
482     void SetNoReturn();
483     bool NeverReturns() const;
484 
485     void SetHasSetjmp();
486     bool HasSetjmp() const;
487 
488     void SetHasAsm();
489     bool HasAsm() const;
490 
491     void SetStructReturnedInRegs();
492     bool StructReturnedInRegs() const;
493 
494     void SetReturnStruct(const MIRType *retType);
495 
496     bool IsEmpty() const;
497     bool IsClinit() const;
498     uint32 GetInfo(GStrIdx strIdx) const;
499     uint32 GetInfo(const std::string &str) const;
IsAFormal(const MIRSymbol * st)500     bool IsAFormal(const MIRSymbol *st) const
501     {
502         for (const auto &formalDef : formalDefVec) {
503             if (st == formalDef.formalSym) {
504                 return true;
505             }
506         }
507         return false;
508     }
509 
GetFormalIndex(const MIRSymbol * symbol)510     uint32 GetFormalIndex(const MIRSymbol *symbol) const
511     {
512         for (size_t i = 0; i < formalDefVec.size(); ++i) {
513             if (formalDefVec[i].formalSym == symbol) {
514                 return i;
515             }
516         }
517         return 0xffffffff;
518     }
519 
GetFormalDefFromMIRSymbol(const MIRSymbol * symbol)520     FormalDef &GetFormalDefFromMIRSymbol(const MIRSymbol *symbol)
521     {
522         for (auto &formalDef : formalDefVec) {
523             if (formalDef.formalSym == symbol) {
524                 return formalDef;
525             }
526         }
527         CHECK_FATAL(false, "Impossible.");
528     }
529 
IsAFormalName(const GStrIdx idx)530     bool IsAFormalName(const GStrIdx idx) const
531     {
532         for (const auto &formalDef : formalDefVec) {
533             if (idx == formalDef.formalStrIdx) {
534                 return true;
535             }
536         }
537         return false;
538     }
539 
GetFormalFromName(const GStrIdx idx)540     const FormalDef GetFormalFromName(const GStrIdx idx) const
541     {
542         for (size_t i = 0; i < formalDefVec.size(); ++i) {
543             if (formalDefVec[i].formalStrIdx == idx) {
544                 return formalDefVec[i];
545             }
546         }
547         return FormalDef();
548     }
549 
550     const MIRType *GetNodeType(const BaseNode &node) const;
551 
552 #ifdef DEBUGME
553     void SetUpGDBEnv();
554     void ResetGDBEnv();
555 #endif
ReleaseMemory()556     void ReleaseMemory()
557     {
558         if (codeMemPoolTmp != nullptr) {
559             delete codeMemPoolTmp;
560             codeMemPoolTmp = nullptr;
561         }
562     }
563 
ReleaseCodeMemory()564     void ReleaseCodeMemory()
565     {
566         if (codeMemPool != nullptr) {
567             codeMemPoolAllocator.SetMemPool(nullptr);
568             delete codeMemPool;
569             SetMemPool(nullptr);
570         }
571     }
572 
GetCodeMempool()573     MemPool *GetCodeMempool()
574     {
575         if (useTmpMemPool) {
576             if (codeMemPoolTmp == nullptr) {
577                 codeMemPoolTmp = new ThreadLocalMemPool(memPoolCtrler, "func code mempool");
578                 codeMemPoolTmpAllocator.SetMemPool(codeMemPoolTmp);
579             }
580             return codeMemPoolTmp;
581         }
582         if (codeMemPool == nullptr) {
583             codeMemPool = new ThreadLocalMemPool(memPoolCtrler, "func code mempool");
584             codeMemPoolAllocator.SetMemPool(codeMemPool);
585         }
586         return codeMemPool;
587     }
588 
GetCodeMemPoolAllocator()589     MapleAllocator &GetCodeMemPoolAllocator()
590     {
591         GetCodeMempool();
592         if (useTmpMemPool) {
593             return codeMemPoolTmpAllocator;
594         }
595         return codeMemPoolAllocator;
596     }
597 
GetCodeMempoolAllocator()598     MapleAllocator &GetCodeMempoolAllocator()
599     {
600         if (codeMemPool == nullptr) {
601             codeMemPool = new ThreadLocalMemPool(memPoolCtrler, "func code mempool");
602             codeMemPoolAllocator.SetMemPool(codeMemPool);
603         }
604         return codeMemPoolAllocator;
605     }
606 
GetFuncRetStructTyIdx()607     TyIdx GetFuncRetStructTyIdx()
608     {
609         TyIdx tyIdx = GetFormalDefAt(0).formalTyIdx;
610         MIRType *ty = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx);
611         CHECK_FATAL(ty->GetKind() == kTypePointer, "Fake param not a pointer");
612         MIRPtrType *pType = static_cast<MIRPtrType *>(ty);
613         tyIdx = pType->GetPointedTyIdx();
614         CHECK_FATAL(GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx)->IsStructType(), "Must be struct return type");
615         return tyIdx;
616     }
617 
618     void EnterFormals();
619     void NewBody();
620 
GetModule()621     MIRModule *GetModule()
622     {
623         return module;
624     }
625 
GetPuidx()626     PUIdx GetPuidx() const
627     {
628         return puIdx;
629     }
SetPuidx(PUIdx idx)630     void SetPuidx(PUIdx idx)
631     {
632         puIdx = idx;
633     }
634 
GetPuidxOrigin()635     PUIdx GetPuidxOrigin() const
636     {
637         return puIdxOrigin;
638     }
SetPuidxOrigin(PUIdx idx)639     void SetPuidxOrigin(PUIdx idx)
640     {
641         puIdxOrigin = idx;
642     }
643 
GetStIdx()644     StIdx GetStIdx() const
645     {
646         return symbolTableIdx;
647     }
SetStIdx(StIdx stIdx)648     void SetStIdx(StIdx stIdx)
649     {
650         symbolTableIdx = stIdx;
651     }
652 
GetSCCId()653     int32 GetSCCId() const
654     {
655         return sccID;
656     }
SetSCCId(int32 id)657     void SetSCCId(int32 id)
658     {
659         sccID = id;
660     }
661 
GetMIRFuncType()662     MIRFuncType *GetMIRFuncType()
663     {
664         return funcType;
665     }
SetMIRFuncType(MIRFuncType * type)666     void SetMIRFuncType(MIRFuncType *type)
667     {
668         funcType = type;
669     }
670 
GetInferredReturnTyIdx()671     TyIdx GetInferredReturnTyIdx() const
672     {
673         return inferredReturnTyIdx;
674     }
675 
SetInferredReturnTyIdx(TyIdx tyIdx)676     void SetInferredReturnTyIdx(TyIdx tyIdx)
677     {
678         inferredReturnTyIdx = tyIdx;
679     }
680 
GetTypeNameTab()681     MIRTypeNameTable *GetTypeNameTab() const
682     {
683         return typeNameTab;
684     }
685 
AllocTypeNameTab()686     void AllocTypeNameTab()
687     {
688         if (typeNameTab == nullptr) {
689             typeNameTab = module->GetMemPool()->New<MIRTypeNameTable>(module->GetMPAllocator());
690         }
691     }
HaveTypeNameTab()692     bool HaveTypeNameTab() const
693     {
694         return typeNameTab != nullptr;
695     }
GetGStrIdxToTyIdxMap()696     const MapleMap<GStrIdx, TyIdx> &GetGStrIdxToTyIdxMap() const
697     {
698         CHECK_FATAL(typeNameTab != nullptr, "typeNameTab is nullptr");
699         return typeNameTab->GetGStrIdxToTyIdxMap();
700     }
GetTyIdxFromGStrIdx(GStrIdx idx)701     TyIdx GetTyIdxFromGStrIdx(GStrIdx idx) const
702     {
703         CHECK_FATAL(typeNameTab != nullptr, "typeNameTab is nullptr");
704         return typeNameTab->GetTyIdxFromGStrIdx(idx);
705     }
SetGStrIdxToTyIdx(GStrIdx gStrIdx,TyIdx tyIdx)706     void SetGStrIdxToTyIdx(GStrIdx gStrIdx, TyIdx tyIdx)
707     {
708         CHECK_FATAL(typeNameTab != nullptr, "typeNameTab is nullptr");
709         typeNameTab->SetGStrIdxToTyIdx(gStrIdx, tyIdx);
710     }
711 
GetLabelTabItem(LabelIdx labelIdx)712     const std::string &GetLabelTabItem(LabelIdx labelIdx) const
713     {
714         CHECK_FATAL(labelTab != nullptr, "labelTab is nullptr");
715         return labelTab->GetName(labelIdx);
716     }
717 
AllocLabelTab()718     void AllocLabelTab()
719     {
720         if (labelTab == nullptr) {
721             labelTab = module->GetMemPool()->New<MIRLabelTable>(module->GetMPAllocator());
722         }
723     }
724 
GetPregTab()725     MIRPregTable *GetPregTab() const
726     {
727         return pregTab;
728     }
729 
SetPregTab(MIRPregTable * tab)730     void SetPregTab(MIRPregTable *tab)
731     {
732         pregTab = tab;
733     }
AllocPregTab()734     void AllocPregTab()
735     {
736         if (pregTab == nullptr) {
737             pregTab = module->GetMemPool()->New<MIRPregTable>(&module->GetMPAllocator());
738         }
739     }
GetPregItem(PregIdx idx)740     MIRPreg *GetPregItem(PregIdx idx)
741     {
742         return const_cast<MIRPreg *>(const_cast<const MIRFunction *>(this)->GetPregItem(idx));
743     }
GetPregItem(PregIdx idx)744     const MIRPreg *GetPregItem(PregIdx idx) const
745     {
746         return pregTab->PregFromPregIdx(idx);
747     }
748 
GetBody()749     BlockNode *GetBody()
750     {
751         return body;
752     }
GetBody()753     const BlockNode *GetBody() const
754     {
755         return body;
756     }
SetBody(BlockNode * node)757     void SetBody(BlockNode *node)
758     {
759         body = node;
760     }
761 
GetLastPosBody()762     BlockNode *GetLastPosBody()
763     {
764         return bodyLast;
765     }
GetLastPosBody()766     const BlockNode *GetLastPosBody() const
767     {
768         return bodyLast;
769     }
SetLastPosBody(BlockNode * node)770     void SetLastPosBody(BlockNode *node)
771     {
772         bodyLast = node;
773     }
774 
GetSrcPosition()775     SrcPosition &GetSrcPosition()
776     {
777         DEBUG_ASSERT(GetFuncSymbol() != nullptr, "null ptr check");
778         return GetFuncSymbol()->GetSrcPosition();
779     }
780 
SetSrcPosition(const SrcPosition & position)781     void SetSrcPosition(const SrcPosition &position)
782     {
783         DEBUG_ASSERT(GetFuncSymbol() != nullptr, "null ptr check");
784         GetFuncSymbol()->SetSrcPosition(position);
785     }
786 
GetFuncAttrs()787     const FuncAttrs &GetFuncAttrs() const
788     {
789         return funcAttrs;
790     }
GetFuncAttrs()791     FuncAttrs &GetFuncAttrs()
792     {
793         return funcAttrs;
794     }
795 
SetFuncAttrs(const FuncAttrs & attrs)796     void SetFuncAttrs(const FuncAttrs &attrs)
797     {
798         funcAttrs = attrs;
799     }
SetFuncAttrs(uint64 attrFlag)800     void SetFuncAttrs(uint64 attrFlag)
801     {
802         funcAttrs.SetAttrFlag(attrFlag);
803     }
804 
GetFlag()805     uint32 GetFlag() const
806     {
807         return flag;
808     }
SetFlag(uint32 newFlag)809     void SetFlag(uint32 newFlag)
810     {
811         flag = newFlag;
812     }
813 
GetHashCode()814     uint16 GetHashCode() const
815     {
816         return hashCode;
817     }
SetHashCode(uint16 newHashCode)818     void SetHashCode(uint16 newHashCode)
819     {
820         hashCode = newHashCode;
821     }
822 
SetFileIndex(uint32 newFileIndex)823     void SetFileIndex(uint32 newFileIndex)
824     {
825         fileIndex = newFileIndex;
826     }
827 
GetInfoVector()828     MIRInfoVector &GetInfoVector()
829     {
830         return info;
831     }
832 
GetInfoPair(size_t i)833     const MIRInfoPair &GetInfoPair(size_t i) const
834     {
835         return info.at(i);
836     }
837 
PushbackMIRInfo(const MIRInfoPair & pair)838     void PushbackMIRInfo(const MIRInfoPair &pair)
839     {
840         info.push_back(pair);
841     }
842 
SetMIRInfoNum(size_t idx,uint32 num)843     void SetMIRInfoNum(size_t idx, uint32 num)
844     {
845         info[idx].second = num;
846     }
847 
InfoIsString()848     MapleVector<bool> &InfoIsString()
849     {
850         return infoIsString;
851     }
852 
PushbackIsString(bool isString)853     void PushbackIsString(bool isString)
854     {
855         infoIsString.push_back(isString);
856     }
857 
GetScope()858     MIRScope *GetScope()
859     {
860         return scope;
861     }
862 
NeedEmitAliasInfo()863     bool NeedEmitAliasInfo() const
864     {
865         return scope->NeedEmitAliasInfo();
866     }
867 
GetAliasVarMap()868     MapleMap<GStrIdx, MIRAliasVars> &GetAliasVarMap()
869     {
870         return scope->GetAliasVarMap();
871     }
872 
SetAliasVarMap(GStrIdx idx,const MIRAliasVars & vars)873     void SetAliasVarMap(GStrIdx idx, const MIRAliasVars &vars)
874     {
875         scope->SetAliasVarMap(idx, vars);
876     }
877 
AddAliasVarMap(GStrIdx idx,const MIRAliasVars & vars)878     void AddAliasVarMap(GStrIdx idx, const MIRAliasVars &vars)
879     {
880         scope->AddAliasVarMap(idx, vars);
881     }
882 
HasVlaOrAlloca()883     bool HasVlaOrAlloca() const
884     {
885         return hasVlaOrAlloca;
886     }
SetVlaOrAlloca(bool has)887     void SetVlaOrAlloca(bool has)
888     {
889         hasVlaOrAlloca = has;
890     }
891 
892     // Default freq is the lastStmtFreq
HasFreqMap()893     bool HasFreqMap() const
894     {
895         return freqLastMap != nullptr;
896     }
897 
HasFirstFreqMap()898     bool HasFirstFreqMap() const
899     {
900         return freqFirstMap != nullptr;
901     }
902 
GetFirstFreqMap()903     const MapleMap<uint32, uint32> &GetFirstFreqMap() const
904     {
905         return *freqFirstMap;
906     }
907 
SetFirstFreqMap(uint32 stmtID,uint32 freq)908     void SetFirstFreqMap(uint32 stmtID, uint32 freq)
909     {
910         if (freqFirstMap == nullptr) {
911             freqFirstMap = module->GetMemPool()->New<MapleMap<uint32, uint32>>(module->GetMPAllocator().Adapter());
912         }
913         (*freqFirstMap)[stmtID] = freq;
914     }
915 
GetLastFreqMap()916     const MapleMap<uint32, uint32> &GetLastFreqMap() const
917     {
918         return *freqLastMap;
919     }
920 
GetFreqFromLastStmt(uint32 stmtId)921     int32 GetFreqFromLastStmt(uint32 stmtId)
922     {
923         if (freqLastMap == nullptr) {
924             return -1;
925         }
926         if ((*freqLastMap).find(stmtId) == (*freqLastMap).end()) {
927             return -1;
928         }
929         return static_cast<int32>((*freqLastMap)[stmtId]);
930     }
931 
GetFreqFromFirstStmt(uint32 stmtId)932     int32 GetFreqFromFirstStmt(uint32 stmtId)
933     {
934         if (freqFirstMap == nullptr) {
935             return -1;
936         }
937         if ((*freqFirstMap).find(stmtId) == (*freqFirstMap).end()) {
938             return -1;
939         }
940         return static_cast<int32>((*freqFirstMap)[stmtId]);
941     }
942 
SetLastFreqMap(uint32 stmtID,uint32 freq)943     void SetLastFreqMap(uint32 stmtID, uint32 freq)
944     {
945         if (freqLastMap == nullptr) {
946             freqLastMap = module->GetMemPool()->New<MapleMap<uint32, uint32>>(module->GetMPAllocator().Adapter());
947         }
948         (*freqLastMap)[stmtID] = freq;
949     }
950 
WithLocInfo()951     bool WithLocInfo() const
952     {
953         return withLocInfo;
954     }
SetWithLocInfo(bool withInfo)955     void SetWithLocInfo(bool withInfo)
956     {
957         withLocInfo = withInfo;
958     }
959 
IsDirty()960     bool IsDirty() const
961     {
962         return isDirty;
963     }
SetDirty(bool dirty)964     void SetDirty(bool dirty)
965     {
966         isDirty = dirty;
967     }
968 
IsFromMpltInline()969     bool IsFromMpltInline() const
970     {
971         return fromMpltInline;
972     }
SetFromMpltInline(bool isInline)973     void SetFromMpltInline(bool isInline)
974     {
975         fromMpltInline = isInline;
976     }
977 
GetLayoutType()978     uint8 GetLayoutType() const
979     {
980         return layoutType;
981     }
SetLayoutType(uint8 type)982     void SetLayoutType(uint8 type)
983     {
984         layoutType = type;
985     }
986 
GetCallTimes()987     uint32 GetCallTimes() const
988     {
989         return callTimes;
990     }
SetCallTimes(uint32 times)991     void SetCallTimes(uint32 times)
992     {
993         callTimes = times;
994     }
995 
GetFrameSize()996     uint32 GetFrameSize() const
997     {
998         return frameSize;
999     }
SetFrameSize(uint32 size)1000     void SetFrameSize(uint32 size)
1001     {
1002         frameSize = size;
1003     }
1004 
GetUpFormalSize()1005     uint32 GetUpFormalSize() const
1006     {
1007         return upFormalSize;
1008     }
SetUpFormalSize(uint32 size)1009     void SetUpFormalSize(uint32 size)
1010     {
1011         upFormalSize = size;
1012     }
1013 
GetOutParmSize()1014     uint32 GetOutParmSize() const
1015     {
1016         return outParmSize;
1017     }
SetOutParmSize(uint32 size)1018     void SetOutParmSize(uint32 size)
1019     {
1020         outParmSize = size;
1021     }
1022 
GetModuleId()1023     uint16 GetModuleId() const
1024     {
1025         return moduleID;
1026     }
SetModuleID(uint16 id)1027     void SetModuleID(uint16 id)
1028     {
1029         moduleID = id;
1030     }
1031 
GetFuncSize()1032     uint32 GetFuncSize() const
1033     {
1034         return funcSize;
1035     }
SetFuncSize(uint32 size)1036     void SetFuncSize(uint32 size)
1037     {
1038         funcSize = size;
1039     }
1040 
GetTempCount()1041     uint32 GetTempCount() const
1042     {
1043         return tempCount;
1044     }
IncTempCount()1045     void IncTempCount()
1046     {
1047         ++tempCount;
1048     }
1049 
GetFormalWordsTypeTagged()1050     uint8 *GetFormalWordsTypeTagged() const
1051     {
1052         return formalWordsTypeTagged;
1053     }
SetFormalWordsTypeTagged(uint8 * tagged)1054     void SetFormalWordsTypeTagged(uint8 *tagged)
1055     {
1056         formalWordsTypeTagged = tagged;
1057     }
GetFwtAddress()1058     uint8 **GetFwtAddress()
1059     {
1060         return &formalWordsTypeTagged;
1061     }
1062 
GetLocalWordsTypeTagged()1063     uint8 *GetLocalWordsTypeTagged() const
1064     {
1065         return localWordsTypeTagged;
1066     }
SetLocalWordsTypeTagged(uint8 * tagged)1067     void SetLocalWordsTypeTagged(uint8 *tagged)
1068     {
1069         localWordsTypeTagged = tagged;
1070     }
GetLwtAddress()1071     uint8 **GetLwtAddress()
1072     {
1073         return &localWordsTypeTagged;
1074     }
1075 
GetFormalWordsRefCounted()1076     uint8 *GetFormalWordsRefCounted() const
1077     {
1078         return formalWordsRefCounted;
1079     }
SetFormalWordsRefCounted(uint8 * counted)1080     void SetFormalWordsRefCounted(uint8 *counted)
1081     {
1082         formalWordsRefCounted = counted;
1083     }
GetFwrAddress()1084     uint8 **GetFwrAddress()
1085     {
1086         return &formalWordsRefCounted;
1087     }
1088 
GetLocalWordsRefCounted()1089     uint8 *GetLocalWordsRefCounted() const
1090     {
1091         return localWordsRefCounted;
1092     }
SetLocalWordsRefCounted(uint8 * counted)1093     void SetLocalWordsRefCounted(uint8 *counted)
1094     {
1095         localWordsRefCounted = counted;
1096     }
1097 
GetMeFunc()1098     MeFunction *GetMeFunc()
1099     {
1100         return meFunc;
1101     }
1102 
SetMeFunc(MeFunction * func)1103     void SetMeFunc(MeFunction *func)
1104     {
1105         meFunc = func;
1106     }
1107 
GetEACG()1108     EAConnectionGraph *GetEACG()
1109     {
1110         return eacg;
1111     }
SetEACG(EAConnectionGraph * eacgVal)1112     void SetEACG(EAConnectionGraph *eacgVal)
1113     {
1114         eacg = eacgVal;
1115     }
1116 
SetFormalDefVec(const MapleVector<FormalDef> & currFormals)1117     void SetFormalDefVec(const MapleVector<FormalDef> &currFormals)
1118     {
1119         formalDefVec = currFormals;
1120     }
1121 
GetFormalDefVec()1122     MapleVector<FormalDef> &GetFormalDefVec()
1123     {
1124         return formalDefVec;
1125     }
1126 
GetFormalDefAt(size_t i)1127     const FormalDef &GetFormalDefAt(size_t i) const
1128     {
1129         return formalDefVec[i];
1130     }
1131 
GetFormalDefAt(size_t i)1132     FormalDef &GetFormalDefAt(size_t i)
1133     {
1134         return formalDefVec[i];
1135     }
1136 
GetFormal(size_t i)1137     const MIRSymbol *GetFormal(size_t i) const
1138     {
1139         return formalDefVec[i].formalSym;
1140     }
1141 
GetFormal(size_t i)1142     MIRSymbol *GetFormal(size_t i)
1143     {
1144         return formalDefVec[i].formalSym;
1145     }
1146 
GetFormalName(size_t i)1147     const std::string &GetFormalName(size_t i) const
1148     {
1149         auto *formal = formalDefVec[i].formalSym;
1150         if (formal != nullptr) {
1151             return formal->GetName();
1152         }
1153         return GlobalTables::GetStrTable().GetStringFromStrIdx(formalDefVec[i].formalStrIdx);
1154     }
1155 
GetFormalCount()1156     size_t GetFormalCount() const
1157     {
1158         return formalDefVec.size();
1159     }
1160 
ClearFormals()1161     void ClearFormals()
1162     {
1163         formalDefVec.clear();
1164     }
1165 
ClearArguments()1166     void ClearArguments()
1167     {
1168         formalDefVec.clear();
1169         funcType->GetParamTypeList().clear();
1170         funcType->GetParamAttrsList().clear();
1171     }
1172 
GetSymbolTabSize()1173     size_t GetSymbolTabSize() const
1174     {
1175         DEBUG_ASSERT(symTab != nullptr, "symTab is nullptr");
1176         return symTab->GetSymbolTableSize();
1177     }
1178     MIRSymbol *GetSymbolTabItem(uint32 idx, bool checkFirst = false) const
1179     {
1180         return symTab->GetSymbolFromStIdx(idx, checkFirst);
1181     }
GetSymTab()1182     const MIRSymbolTable *GetSymTab() const
1183     {
1184         return symTab;
1185     }
GetSymTab()1186     MIRSymbolTable *GetSymTab()
1187     {
1188         return symTab;
1189     }
AllocSymTab()1190     void AllocSymTab()
1191     {
1192         if (symTab == nullptr) {
1193             symTab = module->GetMemPool()->New<MIRSymbolTable>(module->GetMPAllocator());
1194         }
1195     }
GetLabelTab()1196     MIRLabelTable *GetLabelTab() const
1197     {
1198         CHECK_FATAL(labelTab != nullptr, "must be");
1199         return labelTab;
1200     }
GetLabelTab()1201     MIRLabelTable *GetLabelTab()
1202     {
1203         if (labelTab == nullptr) {
1204             labelTab = module->GetMemPool()->New<MIRLabelTable>(module->GetMPAllocator());
1205         }
1206         return labelTab;
1207     }
SetLabelTab(MIRLabelTable * currLabelTab)1208     void SetLabelTab(MIRLabelTable *currLabelTab)
1209     {
1210         labelTab = currLabelTab;
1211     }
1212 
GetRetRefSym()1213     const MapleSet<MIRSymbol *> &GetRetRefSym() const
1214     {
1215         return retRefSym;
1216     }
InsertMIRSymbol(MIRSymbol * sym)1217     void InsertMIRSymbol(MIRSymbol *sym)
1218     {
1219         (void)retRefSym.insert(sym);
1220     }
1221 
GetDataMemPool()1222     MemPool *GetDataMemPool() const
1223     {
1224         return module->GetMemPool();
1225     }
1226 
GetCodeMemPool()1227     MemPool *GetCodeMemPool()
1228     {
1229         if (codeMemPool == nullptr) {
1230             codeMemPool = new ThreadLocalMemPool(memPoolCtrler, "func code mempool");
1231             codeMemPoolAllocator.SetMemPool(codeMemPool);
1232         }
1233         return codeMemPool;
1234     }
1235 
SetCodeMemPool(MemPool * currCodeMemPool)1236     void SetCodeMemPool(MemPool *currCodeMemPool)
1237     {
1238         codeMemPool = currCodeMemPool;
1239     }
1240 
GetCodeMPAllocator()1241     MapleAllocator &GetCodeMPAllocator()
1242     {
1243         GetCodeMemPool();
1244         return codeMemPoolAllocator;
1245     }
1246 
AddFuncGenericDeclare(GenericDeclare * g)1247     void AddFuncGenericDeclare(GenericDeclare *g)
1248     {
1249         genericDeclare.push_back(g);
1250     }
1251 
AddFuncGenericArg(AnnotationType * a)1252     void AddFuncGenericArg(AnnotationType *a)
1253     {
1254         genericArg.push_back(a);
1255     }
1256 
AddFuncGenericRet(AnnotationType * r)1257     void AddFuncGenericRet(AnnotationType *r)
1258     {
1259         genericRet = r;
1260     }
1261 
AddFuncLocalGenericVar(const GStrIdx & str,AnnotationType * at)1262     void AddFuncLocalGenericVar(const GStrIdx &str, AnnotationType *at)
1263     {
1264         genericLocalVar[str] = at;
1265     }
1266 
GetFuncGenericDeclare()1267     MapleVector<GenericDeclare *> &GetFuncGenericDeclare()
1268     {
1269         return genericDeclare;
1270     }
1271 
GetFuncGenericArg()1272     MapleVector<AnnotationType *> &GetFuncGenericArg()
1273     {
1274         return genericArg;
1275     }
1276 
SetRetrunAttrKind(const PointerAttr kind)1277     void SetRetrunAttrKind(const PointerAttr kind)
1278     {
1279         returnKind = kind;
1280     }
1281 
GetRetrunAttrKind()1282     PointerAttr GetRetrunAttrKind() const
1283     {
1284         return returnKind;
1285     }
1286 
GetFuncGenericRet()1287     AnnotationType *GetFuncGenericRet()
1288     {
1289         return genericRet;
1290     }
1291 
GetFuncLocalGenericVar(const GStrIdx & str)1292     AnnotationType *GetFuncLocalGenericVar(const GStrIdx &str)
1293     {
1294         if (genericLocalVar.find(str) == genericLocalVar.end()) {
1295             return nullptr;
1296         }
1297         return genericLocalVar[str];
1298     }
1299 
FindStmtWithId(StmtNode * stmt,uint32 stmtId)1300     StmtNode *FindStmtWithId(StmtNode *stmt, uint32 stmtId)
1301     {
1302         while (stmt != nullptr) {
1303             StmtNode *next = stmt->GetNext();
1304             switch (stmt->GetOpCode()) {
1305                 case OP_dowhile:
1306                 case OP_while: {
1307                     WhileStmtNode *wnode = static_cast<WhileStmtNode *>(stmt);
1308                     if (wnode->GetBody() != nullptr && wnode->GetBody()->GetFirst() != nullptr) {
1309                         StmtNode *res = FindStmtWithId(wnode->GetBody()->GetFirst(), stmtId);
1310                         if (res != nullptr) {
1311                             return res;
1312                         }
1313                     }
1314                     break;
1315                 }
1316                 case OP_if: {
1317                     if (stmt->GetMeStmtID() == stmtId) {
1318                         return stmt;
1319                     }
1320                     IfStmtNode *inode = static_cast<IfStmtNode *>(stmt);
1321                     if (inode->GetThenPart() != nullptr && inode->GetThenPart()->GetFirst() != nullptr) {
1322                         StmtNode *res = FindStmtWithId(inode->GetThenPart()->GetFirst(), stmtId);
1323                         if (res != nullptr) {
1324                             return res;
1325                         }
1326                     }
1327                     if (inode->GetElsePart() != nullptr && inode->GetElsePart()->GetFirst() != nullptr) {
1328                         StmtNode *res = FindStmtWithId(inode->GetElsePart()->GetFirst(), stmtId);
1329                         if (res != nullptr) {
1330                             return res;
1331                         }
1332                     }
1333                     break;
1334                 }
1335                 case OP_callassigned:
1336                 case OP_call:
1337                 case OP_brtrue:
1338                 case OP_brfalse: {
1339                     if (stmt->GetMeStmtID() == stmtId) {
1340                         return stmt;
1341                     }
1342                     break;
1343                 }
1344                 default: {
1345                     break;
1346                 }
1347             }
1348             stmt = next;
1349         }
1350         return nullptr;
1351     }
1352 
GetStmtNodeFromMeId(uint32 stmtId)1353     StmtNode *GetStmtNodeFromMeId(uint32 stmtId)
1354     {
1355         if (GetBody() == nullptr) {
1356             return nullptr;
1357         }
1358         StmtNode *stmt = GetBody()->GetFirst();
1359         return FindStmtWithId(stmt, stmtId);
1360     }
1361 
GetCodeMemPoolTmp()1362     MemPool *GetCodeMemPoolTmp()
1363     {
1364         if (codeMemPoolTmp == nullptr) {
1365             codeMemPoolTmp = new ThreadLocalMemPool(memPoolCtrler, "func code mempool");
1366             codeMemPoolTmpAllocator.SetMemPool(codeMemPoolTmp);
1367         }
1368         return codeMemPoolTmp;
1369     }
1370 
CheckParamNullType(MIRSymbol * sym)1371     bool CheckParamNullType(MIRSymbol *sym)
1372     {
1373         return paramNonullTypeMap.find(sym) != paramNonullTypeMap.end();
1374     }
1375 
GetParamNonull(MIRSymbol * sym)1376     PointerAttr GetParamNonull(MIRSymbol *sym)
1377     {
1378         return paramNonullTypeMap[sym];
1379     }
1380 
SetParamNonull(MIRSymbol * sym,PointerAttr type)1381     void SetParamNonull(MIRSymbol *sym, PointerAttr type)
1382     {
1383         paramNonullTypeMap[sym] = type;
1384     }
1385 
CopyReferedRegs(std::set<uint32> regs)1386     void CopyReferedRegs(std::set<uint32> regs)
1387     {
1388         for (auto reg : regs) {
1389             referedPregs.insert(reg);
1390         }
1391     }
1392 
GetReferedRegs()1393     MapleSet<uint32> GetReferedRegs() const
1394     {
1395         return referedPregs;
1396     }
1397 
SetDerived2BaseRef(PregIdx deriveRef,PregIdx baseRef)1398     void SetDerived2BaseRef(PregIdx deriveRef, PregIdx baseRef)
1399     {
1400         CHECK_FATAL(derived2BaseRef.find(deriveRef) == derived2BaseRef.end(), "derived2BaseRef double set");
1401         derived2BaseRef[deriveRef] = baseRef;
1402     }
1403 
GetDerived2BaseRef()1404     const MapleUnorderedMap<PregIdx, PregIdx> &GetDerived2BaseRef() const
1405     {
1406         return derived2BaseRef;
1407     }
1408 
IsReferedRegsValid()1409     bool IsReferedRegsValid() const
1410     {
1411         return referedRegsValid;
1412     }
1413 
SetReferedRegsValid(bool val)1414     void SetReferedRegsValid(bool val)
1415     {
1416         referedRegsValid = val;
1417     }
1418 
GetFuncDesc()1419     FuncDesc &GetFuncDesc()
1420     {
1421         return funcDesc;
1422     }
1423 
SetFuncDesc(const FuncDesc & value)1424     void SetFuncDesc(const FuncDesc &value)
1425     {
1426         funcDesc = value;
1427     }
1428 
SetProfCtrTbl(MIRSymbol * pct)1429     void SetProfCtrTbl(MIRSymbol *pct)
1430     {
1431         CHECK_FATAL(Options::profileGen, "This is only for profileGen");
1432         profCtrTbl = pct;
1433     }
1434 
GetProfCtrTbl()1435     MIRSymbol *GetProfCtrTbl()
1436     {
1437         return profCtrTbl;
1438     }
1439 
SetNumCtrs(uint32 num)1440     void SetNumCtrs(uint32 num)
1441     {
1442         CHECK_FATAL(Options::profileGen, "This is only for profileGen");
1443         nCtrs = num;
1444     }
1445 
GetNumCtrs()1446     uint32 GetNumCtrs() const
1447     {
1448         return nCtrs;
1449     }
1450 
SetFileLineNoChksum(uint64 chksum)1451     void SetFileLineNoChksum(uint64 chksum)
1452     {
1453         CHECK_FATAL(Options::profileGen, "This is only for profileGen");
1454         fileLinenoChksum = chksum;
1455     }
1456 
GetFileLineNoChksum()1457     uint64 GetFileLineNoChksum() const
1458     {
1459         return fileLinenoChksum;
1460     }
1461 
SetCFGChksum(uint64 chksum)1462     void SetCFGChksum(uint64 chksum)
1463     {
1464         CHECK_FATAL(Options::profileGen, "This is only for profileGen");
1465         cfgChksum = chksum;
1466     }
1467 
GetCFGChksum()1468     uint64 GetCFGChksum() const
1469     {
1470         return cfgChksum;
1471     }
1472 
InitFuncDescToBest()1473     void InitFuncDescToBest()
1474     {
1475         funcDesc.InitToBest();
1476     }
1477 
GetFuncDesc()1478     const FuncDesc &GetFuncDesc() const
1479     {
1480         return funcDesc;
1481     }
1482 
AddProfileDesc(uint64 hash,uint32 start,uint32 end)1483     void AddProfileDesc(uint64 hash, uint32 start, uint32 end)
1484     {
1485         profileDesc = module->GetMemPool()->New<IRProfileDesc>(hash, start, end);
1486     }
1487 
GetProfInf()1488     const IRProfileDesc *GetProfInf()
1489     {
1490         if (profileDesc == nullptr) {
1491             // return profileDesc with default value
1492             profileDesc = module->GetMemPool()->New<IRProfileDesc>();
1493         }
1494         return profileDesc;
1495     }
1496 
IsVisited()1497     bool IsVisited() const
1498     {
1499         return isVisited;
1500     }
SetIsVisited()1501     void SetIsVisited()
1502     {
1503         isVisited = true;
1504     }
1505 
SetFuncProfData(GcovFuncInfo * data)1506     void SetFuncProfData(GcovFuncInfo *data)
1507     {
1508         funcProfData = data;
1509     }
GetFuncProfData()1510     GcovFuncInfo *GetFuncProfData()
1511     {
1512         return funcProfData;
1513     }
GetFuncProfData()1514     GcovFuncInfo *GetFuncProfData() const
1515     {
1516         return funcProfData;
1517     }
SetStmtFreq(uint32_t stmtID,uint64_t freq)1518     void SetStmtFreq(uint32_t stmtID, uint64_t freq)
1519     {
1520         DEBUG_ASSERT((funcProfData != nullptr && freq > 0), "nullptr check");
1521         funcProfData->SetStmtFreq(stmtID, static_cast<int64_t>(freq));
1522     }
1523 
SetWithSrc(bool var)1524     void SetWithSrc(bool var)
1525     {
1526         withSrc = var;
1527     }
1528 
GetWithSrc()1529     bool GetWithSrc() const
1530     {
1531         return withSrc;
1532     }
1533 
GetFrameReseverdSlot()1534     uint8 GetFrameReseverdSlot()
1535     {
1536         return funcAttrs.GetFrameResverdSlot();
1537     }
1538 
GetMemReferenceTable()1539     MemReferenceTable *GetMemReferenceTable()
1540     {
1541         return memReferenceTable;
1542     }
1543 
DiscardMemReferenceTable()1544     void DiscardMemReferenceTable()
1545     {
1546         memReferenceTable = nullptr;
1547     }
1548 
1549     void CreateMemReferenceTable();
1550 
1551     MemReferenceTable *GetOrCreateMemReferenceTable();
1552 
AddNewDebugComment(const std::string & dbgComment)1553     const MapleString* AddNewDebugComment(const std::string& dbgComment)
1554     {
1555         return &debugComments.emplace_back(dbgComment, module->GetMPAllocator().GetMemPool());
1556     }
1557 
1558 private:
1559     MIRModule *module;      // the module that owns this function
1560     PUIdx puIdx = 0;        // the PU index of this function
1561     PUIdx puIdxOrigin = 0;  // the original puIdx when initial generation
1562     StIdx symbolTableIdx;   // the symbol table index of this function
1563     int32 sccID = -1;       // the scc id of this function, for mplipa
1564     MIRFuncType *funcType = nullptr;
1565     TyIdx inferredReturnTyIdx {0};  // the actual return type of of this function (may be a
1566                                     // subclass of the above). 0 means can not be inferred.
1567     TyIdx classTyIdx {0};           // class/interface type this function belongs to
1568     MapleVector<FormalDef> formalDefVec {module->GetMPAllocator().Adapter()};  // the formals in function definition
1569     MapleSet<MIRSymbol *> retRefSym {module->GetMPAllocator().Adapter()};
1570 
1571     MapleVector<GenericDeclare *> genericDeclare {module->GetMPAllocator().Adapter()};
1572     MapleVector<AnnotationType *> genericArg {module->GetMPAllocator().Adapter()};
1573     MapleMap<GStrIdx, AnnotationType *> genericLocalVar {module->GetMPAllocator().Adapter()};
1574     AnnotationType *genericRet = nullptr;
1575 
1576     MIRSymbolTable *symTab = nullptr;
1577     MIRTypeNameTable *typeNameTab = nullptr;
1578     MIRLabelTable *labelTab = nullptr;
1579     MIRPregTable *pregTab = nullptr;
1580     MemPool *codeMemPool = nullptr;
1581     MapleAllocator codeMemPoolAllocator {nullptr};
1582     uint32 callTimes = 0;
1583     BlockNode *body = nullptr;
1584     BlockNode *bodyLast = nullptr;
1585     FuncAttrs funcAttrs {};
1586     uint32 flag = 0;
1587     uint16 hashCode = 0;   // for methodmetadata order
1588     uint32 fileIndex = 0;  // this function belongs to which file, used by VM for plugin manager
1589     MIRInfoVector info {module->GetMPAllocator().Adapter()};
1590     MapleVector<bool> infoIsString {module->GetMPAllocator().Adapter()};  // tells if an entry has string value
1591     MIRScope *scope = nullptr;
1592     MapleMap<uint32, uint32> *freqFirstMap = nullptr;  // save bb frequency in its first_stmt, key is stmtId
1593     MapleMap<uint32, uint32> *freqLastMap = nullptr;   // save bb frequency in its last_stmt, key is stmtId
1594     MapleSet<uint32> referedPregs {module->GetMPAllocator().Adapter()};
1595     MapleUnorderedMap<PregIdx, PregIdx> derived2BaseRef {module->GetMPAllocator().Adapter()};
1596     bool referedRegsValid = false;
1597     bool hasVlaOrAlloca = false;
1598     bool withLocInfo = true;
1599     bool withSrc = true;
1600     bool isVisited = false;  // only used in inline phase.
1601     bool isDirty = false;
1602     bool fromMpltInline = false;  // Whether this function is imported from mplt_inline file or not.
1603     uint8_t layoutType = kLayoutUnused;
1604     uint32 frameSize = 0;
1605     uint32 upFormalSize = 0;
1606     uint32 outParmSize = 0;
1607     uint16 moduleID = 0;
1608     uint32 funcSize = 0;  // size of code in words
1609     uint32 tempCount = 0;
1610     uint8 *formalWordsTypeTagged = nullptr;  // bit vector where the Nth bit tells whether
1611     // the Nth word in the formal parameters area
1612     // addressed upward from %%FP (that means
1613     // the word at location (%%FP + N*4)) has
1614     // typetag; if yes, the typetag is the word
1615     // at (%%FP + N*4 + 4); the bitvector's size
1616     // is given by BlockSize2BitvectorSize(upFormalSize)
1617     uint8 *localWordsTypeTagged = nullptr;  // bit vector where the Nth bit tells whether
1618     // the Nth word in the local stack frame
1619     // addressed downward from %%FP (that means
1620     // the word at location (%%FP - N*4)) has
1621     // typetag; if yes, the typetag is the word
1622     // at (%%FP - N*4 + 4); the bitvector's size
1623     // is given by BlockSize2BitvectorSize(frameSize)
1624     uint8 *formalWordsRefCounted = nullptr;  // bit vector where the Nth bit tells whether
1625     // the Nth word in the formal parameters area
1626     // addressed upward from %%FP (that means
1627     // the word at location (%%FP + N*4)) points to
1628     // a dynamic memory block that needs reference
1629     // count; the bitvector's size is given by
1630     // BlockSize2BitvectorSize(upFormalSize)
1631     uint8 *localWordsRefCounted = nullptr;  // bit vector where the Nth bit tells whether
1632     // the Nth word in the local stack frame
1633     // addressed downward from %%FP (that means
1634     // the word at location (%%FP - N*4)) points to
1635     // a dynamic memory block that needs reference
1636     // count; the bitvector's size is given by
1637     // BlockSize2BitvectorSize(frameSize)
1638     // uint16 numlabels // removed. label table size
1639     // StmtNode **lbl2stmt // lbl2stmt table, removed
1640     // to hold unmangled class and function names
1641     MeFunction *meFunc = nullptr;
1642     EAConnectionGraph *eacg = nullptr;
1643     IRProfileDesc *profileDesc = nullptr;
1644     GStrIdx baseClassStrIdx {0};  // the string table index of base class name
1645     GStrIdx baseFuncStrIdx {0};   // the string table index of base function name
1646     // the string table index of base function name mangled with type info
1647     GStrIdx baseFuncWithTypeStrIdx {0};
1648     // funcname + types of args, no type of retv
1649     GStrIdx baseFuncSigStrIdx {0};
1650     GStrIdx signatureStrIdx {0};
1651     MemPool *codeMemPoolTmp {nullptr};
1652     MapleAllocator codeMemPoolTmpAllocator {nullptr};
1653     bool useTmpMemPool = false;
1654     PointerAttr returnKind = PointerAttr::kPointerUndeiced;
1655     MapleMap<MIRSymbol *, PointerAttr> paramNonullTypeMap {module->GetMPAllocator().Adapter()};
1656     FuncDesc funcDesc {};
1657     MIRSymbol *profCtrTbl = nullptr;
1658     uint32 nCtrs = 0;  // number of counters
1659     uint64 fileLinenoChksum = 0;
1660     uint64 cfgChksum = 0;
1661     GcovFuncInfo *funcProfData = nullptr;
1662     MemReferenceTable *memReferenceTable = nullptr;
1663     void DumpFlavorLoweredThanMmpl() const;
1664     MIRFuncType *ReconstructFormals(const std::vector<MIRSymbol *> &symbols, bool clearOldArgs);
1665     MapleList<MapleString> debugComments {module->GetMPAllocator().Adapter()};
1666 };
1667 }  // namespace maple
1668 #endif  // MAPLE_IR_INCLUDE_MIR_FUNCTION_H
1669