1 //===- subzero/src/IceCfg.h - Control flow graph ----------------*- C++ -*-===// 2 // 3 // The Subzero Code Generator 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Declares the Cfg class, which represents the control flow graph and 12 /// the overall per-function compilation context. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #ifndef SUBZERO_SRC_ICECFG_H 17 #define SUBZERO_SRC_ICECFG_H 18 19 #include "IceAssembler.h" 20 #include "IceClFlags.h" 21 #include "IceDefs.h" 22 #include "IceGlobalContext.h" 23 #include "IceLoopAnalyzer.h" 24 #include "IceStringPool.h" 25 #include "IceTypes.h" 26 27 namespace Ice { 28 29 class Cfg { 30 Cfg() = delete; 31 Cfg(const Cfg &) = delete; 32 Cfg &operator=(const Cfg &) = delete; 33 34 std::unique_ptr<ArenaAllocator> Allocator; 35 36 public: 37 ~Cfg(); 38 create(GlobalContext * Ctx,uint32_t SequenceNumber)39 static std::unique_ptr<Cfg> create(GlobalContext *Ctx, 40 uint32_t SequenceNumber) { 41 return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber)); 42 } 43 getContext()44 GlobalContext *getContext() const { return Ctx; } getSequenceNumber()45 uint32_t getSequenceNumber() const { return SequenceNumber; } getOptLevel()46 OptLevel getOptLevel() const { return OptimizationLevel; } 47 defaultVerboseMask()48 static constexpr VerboseMask defaultVerboseMask() { 49 return (IceV_NO_PER_PASS_DUMP_BEYOND << 1) - 1; 50 } 51 /// Returns true if any of the specified options in the verbose mask are set. 52 /// If the argument is omitted, it checks if any verbose options at all are 53 /// set. 54 bool isVerbose(VerboseMask Mask = defaultVerboseMask()) const { 55 return VMask & Mask; 56 } setVerbose(VerboseMask Mask)57 void setVerbose(VerboseMask Mask) { VMask = Mask; } 58 59 /// \name Manage the name and return type of the function being translated. 60 /// @{ setFunctionName(GlobalString Name)61 void setFunctionName(GlobalString Name) { FunctionName = Name; } getFunctionName()62 GlobalString getFunctionName() const { return FunctionName; } 63 std::string getFunctionNameAndSize() const; setReturnType(Type Ty)64 void setReturnType(Type Ty) { ReturnType = Ty; } getReturnType()65 Type getReturnType() const { return ReturnType; } 66 /// @} 67 68 /// \name Manage the "internal" attribute of the function. 69 /// @{ setInternal(bool Internal)70 void setInternal(bool Internal) { IsInternalLinkage = Internal; } getInternal()71 bool getInternal() const { return IsInternalLinkage; } 72 /// @} 73 74 /// \name Manage errors. 75 /// @{ 76 77 /// Translation error flagging. If support for some construct is known to be 78 /// missing, instead of an assertion failure, setError() should be called and 79 /// the error should be propagated back up. This way, we can gracefully fail 80 /// to translate and let a fallback translator handle the function. 81 void setError(const std::string &Message); hasError()82 bool hasError() const { return HasError; } getError()83 std::string getError() const { return ErrorMessage; } 84 /// @} 85 86 /// \name Manage nodes (a.k.a. basic blocks, CfgNodes). 87 /// @{ setEntryNode(CfgNode * EntryNode)88 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; } getEntryNode()89 CfgNode *getEntryNode() const { return Entry; } 90 /// Create a node and append it to the end of the linearized list. The loop 91 /// nest depth of the new node may not be valid if it is created after 92 /// computeLoopNestDepth. 93 CfgNode *makeNode(); getNumNodes()94 SizeT getNumNodes() const { return Nodes.size(); } getNodes()95 const NodeList &getNodes() const { return Nodes; } 96 /// Swap nodes of Cfg with given list of nodes. The number of nodes must 97 /// remain unchanged. 98 void swapNodes(NodeList &NewNodes); 99 /// @} 100 101 /// String pool for CfgNode::Name values. getNodeStrings()102 StringPool *getNodeStrings() const { return NodeStrings.get(); } 103 /// String pool for Variable::Name values. getVarStrings()104 StringPool *getVarStrings() const { return VarStrings.get(); } 105 106 /// \name Manage instruction numbering. 107 /// @{ newInstNumber()108 InstNumberT newInstNumber() { return NextInstNumber++; } getNextInstNumber()109 InstNumberT getNextInstNumber() const { return NextInstNumber; } 110 /// @} 111 112 /// \name Manage Variables. 113 /// @{ 114 115 /// Create a new Variable with a particular type and an optional name. The 116 /// Node argument is the node where the variable is defined. 117 // TODO(jpp): untemplate this with separate methods: makeVariable and 118 // makeStackVariable. makeVariable(Type Ty)119 template <typename T = Variable> T *makeVariable(Type Ty) { 120 SizeT Index = Variables.size(); 121 auto *Var = T::create(this, Ty, Index); 122 Variables.push_back(Var); 123 return Var; 124 } getNumVariables()125 SizeT getNumVariables() const { return Variables.size(); } getVariables()126 const VarList &getVariables() const { return Variables; } 127 /// @} 128 129 /// \name Manage arguments to the function. 130 /// @{ 131 void addArg(Variable *Arg); getArgs()132 const VarList &getArgs() const { return Args; } getArgs()133 VarList &getArgs() { return Args; } 134 void addImplicitArg(Variable *Arg); getImplicitArgs()135 const VarList &getImplicitArgs() const { return ImplicitArgs; } 136 /// @} 137 138 /// \name Manage the jump tables. 139 /// @{ addJumpTable(InstJumpTable * JumpTable)140 void addJumpTable(InstJumpTable *JumpTable) { 141 JumpTables.emplace_back(JumpTable); 142 } 143 /// @} 144 145 /// \name Manage the Globals used by this function. 146 /// @{ getGlobalInits()147 std::unique_ptr<VariableDeclarationList> getGlobalInits() { 148 return std::move(GlobalInits); 149 } addGlobal(VariableDeclaration * Global)150 void addGlobal(VariableDeclaration *Global) { 151 if (GlobalInits == nullptr) { 152 GlobalInits.reset(new VariableDeclarationList); 153 } 154 GlobalInits->push_back(Global); 155 } getGlobalPool()156 VariableDeclarationList *getGlobalPool() { 157 if (GlobalInits == nullptr) { 158 GlobalInits.reset(new VariableDeclarationList); 159 } 160 return GlobalInits.get(); 161 } 162 /// @} 163 164 /// \name Miscellaneous accessors. 165 /// @{ getTarget()166 TargetLowering *getTarget() const { return Target.get(); } getVMetadata()167 VariablesMetadata *getVMetadata() const { return VMetadata.get(); } getLiveness()168 Liveness *getLiveness() const { return Live.get(); } getAssembler()169 template <typename T = Assembler> T *getAssembler() const { 170 return llvm::dyn_cast<T>(TargetAssembler.get()); 171 } releaseAssembler()172 std::unique_ptr<Assembler> releaseAssembler() { 173 return std::move(TargetAssembler); 174 } 175 bool hasComputedFrame() const; getFocusedTiming()176 bool getFocusedTiming() const { return FocusedTiming; } setFocusedTiming()177 void setFocusedTiming() { FocusedTiming = true; } 178 /// @} 179 180 /// Passes over the CFG. 181 void translate(); 182 /// After the CFG is fully constructed, iterate over the nodes and compute the 183 /// predecessor and successor edges, in the form of CfgNode::InEdges[] and 184 /// CfgNode::OutEdges[]. 185 void computeInOutEdges(); 186 /// Renumber the non-deleted instructions in the Cfg. This needs to be done 187 /// in preparation for live range analysis. The instruction numbers in a 188 /// block must be monotonically increasing. The range of instruction numbers 189 /// in a block, from lowest to highest, must not overlap with the range of any 190 /// other block. 191 /// 192 /// Also, if the configuration specifies to do so, remove/unlink all deleted 193 /// instructions from the Cfg, to speed up later passes over the instructions. 194 void renumberInstructions(); 195 void placePhiLoads(); 196 void placePhiStores(); 197 void deletePhis(); 198 void advancedPhiLowering(); 199 void reorderNodes(); 200 void localCSE(bool AssumeSSA); 201 void floatConstantCSE(); 202 void shortCircuitJumps(); 203 void loopInvariantCodeMotion(); 204 205 /// Scan allocas to determine whether we need to use a frame pointer. 206 /// If SortAndCombine == true, merge all the fixed-size allocas in the 207 /// entry block and emit stack or frame pointer-relative addressing. 208 void processAllocas(bool SortAndCombine); 209 void doAddressOpt(); 210 /// Find clusters of insertelement/extractelement instructions that can be 211 /// replaced by a shufflevector instruction. 212 void materializeVectorShuffles(); 213 void doArgLowering(); 214 void genCode(); 215 void genFrame(); 216 void generateLoopInfo(); 217 void livenessLightweight(); 218 void liveness(LivenessMode Mode); 219 bool validateLiveness() const; 220 void contractEmptyNodes(); 221 void doBranchOpt(); 222 223 /// \name Manage the CurrentNode field. 224 /// CurrentNode is used for validating the Variable::DefNode field during 225 /// dumping/emitting. 226 /// @{ setCurrentNode(const CfgNode * Node)227 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } resetCurrentNode()228 void resetCurrentNode() { setCurrentNode(nullptr); } getCurrentNode()229 const CfgNode *getCurrentNode() const { return CurrentNode; } 230 /// @} 231 232 /// Get the total amount of memory held by the per-Cfg allocator. 233 size_t getTotalMemoryMB() const; 234 235 /// Get the current memory usage due to liveness data structures. 236 size_t getLivenessMemoryMB() const; 237 238 void emit(); 239 void emitIAS(); 240 static void emitTextHeader(GlobalString Name, GlobalContext *Ctx, 241 const Assembler *Asm); 242 void dump(const char *Message = ""); 243 244 /// Allocate data of type T using the per-Cfg allocator. allocate()245 template <typename T> T *allocate() { return Allocator->Allocate<T>(); } 246 247 /// Allocate an array of data of type T using the per-Cfg allocator. allocateArrayOf(size_t NumElems)248 template <typename T> T *allocateArrayOf(size_t NumElems) { 249 return Allocator->Allocate<T>(NumElems); 250 } 251 252 /// Deallocate data that was allocated via allocate<T>(). deallocate(T * Object)253 template <typename T> void deallocate(T *Object) { 254 Allocator->Deallocate(Object); 255 } 256 257 /// Deallocate data that was allocated via allocateArrayOf<T>(). deallocateArrayOf(T * Array)258 template <typename T> void deallocateArrayOf(T *Array) { 259 Allocator->Deallocate(Array); 260 } 261 262 /// Update Phi labels with InEdges. 263 /// 264 /// The WASM translator cannot always determine the right incoming edge for a 265 /// value due to the CFG being built incrementally. The fixPhiNodes pass fills 266 /// in the correct information once everything is known. 267 void fixPhiNodes(); 268 setStackSizeLimit(uint32_t Limit)269 void setStackSizeLimit(uint32_t Limit) { StackSizeLimit = Limit; } getStackSizeLimit()270 uint32_t getStackSizeLimit() const { return StackSizeLimit; } 271 272 private: 273 friend class CfgAllocatorTraits; // for Allocator access. 274 275 Cfg(GlobalContext *Ctx, uint32_t SequenceNumber); 276 277 void createNodeNameDeclaration(const std::string &NodeAsmName); 278 void 279 createBlockProfilingInfoDeclaration(const std::string &NodeAsmName, 280 VariableDeclaration *NodeNameDeclaration); 281 282 /// Iterate through the registered jump tables and emit them. 283 void emitJumpTables(); 284 285 enum AllocaBaseVariableType { 286 BVT_StackPointer, 287 BVT_FramePointer, 288 BVT_UserPointer 289 }; 290 void sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas, 291 uint32_t CombinedAlignment, InstList &Insts, 292 AllocaBaseVariableType BaseVariableType); 293 void findRematerializable(); 294 CfgVector<Inst *> 295 findLoopInvariantInstructions(const CfgUnorderedSet<SizeT> &Body); 296 297 static ArenaAllocator *createAllocator(); 298 299 GlobalContext *Ctx; 300 uint32_t SequenceNumber; /// output order for emission 301 OptLevel OptimizationLevel = Opt_m1; 302 VerboseMask VMask; 303 GlobalString FunctionName; 304 Type ReturnType = IceType_void; 305 bool IsInternalLinkage = false; 306 bool HasError = false; 307 bool FocusedTiming = false; 308 std::string ErrorMessage = ""; 309 CfgNode *Entry = nullptr; /// entry basic block 310 NodeList Nodes; /// linearized node list; Entry should be first 311 InstNumberT NextInstNumber; 312 VarList Variables; 313 VarList Args; /// subset of Variables, in argument order 314 VarList ImplicitArgs; /// subset of Variables 315 // Separate string pools for CfgNode and Variable names, due to a combination 316 // of the uniqueness requirement, and assumptions in lit tests. 317 std::unique_ptr<StringPool> NodeStrings; 318 std::unique_ptr<StringPool> VarStrings; 319 std::unique_ptr<Liveness> Live; 320 std::unique_ptr<TargetLowering> Target; 321 std::unique_ptr<VariablesMetadata> VMetadata; 322 std::unique_ptr<Assembler> TargetAssembler; 323 /// Globals required by this CFG. 324 std::unique_ptr<VariableDeclarationList> GlobalInits; 325 CfgVector<InstJumpTable *> JumpTables; 326 /// CurrentNode is maintained during dumping/emitting just for validating 327 /// Variable::DefNode. Normally, a traversal over CfgNodes maintains this, but 328 /// before global operations like register allocation, resetCurrentNode() 329 /// should be called to avoid spurious validation failures. 330 const CfgNode *CurrentNode = nullptr; 331 CfgVector<Loop> LoopInfo; 332 uint32_t StackSizeLimit = 1 * 1024 * 1024; // 1 MiB 333 334 public: TlsInit()335 static void TlsInit() { CfgAllocatorTraits::init(); } 336 }; 337 338 template <> Variable *Cfg::makeVariable<Variable>(Type Ty); 339 340 struct NodeStringPoolTraits { 341 using OwnerType = Cfg; getStringsNodeStringPoolTraits342 static StringPool *getStrings(const OwnerType *PoolOwner) { 343 return PoolOwner->getNodeStrings(); 344 } 345 }; 346 using NodeString = StringID<NodeStringPoolTraits>; 347 348 struct VariableStringPoolTraits { 349 using OwnerType = Cfg; getStringsVariableStringPoolTraits350 static StringPool *getStrings(const OwnerType *PoolOwner) { 351 return PoolOwner->getVarStrings(); 352 } 353 }; 354 using VariableString = StringID<VariableStringPoolTraits>; 355 356 } // end of namespace Ice 357 358 #endif // SUBZERO_SRC_ICECFG_H 359