blob: a743cbccb3d66706298ffefb6cdb8a4017a11896 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file provides interfaces used to build and manipulate a call graph,
11/// which is a very useful tool for interprocedural optimization.
12///
13/// Every function in a module is represented as a node in the call graph. The
14/// callgraph node keeps track of which functions are called by the function
15/// corresponding to the node.
16///
17/// A call graph may contain nodes where the function that they correspond to
18/// is null. These 'external' nodes are used to represent control flow that is
19/// not represented (or analyzable) in the module. In particular, this
20/// analysis builds one external node such that:
21/// 1. All functions in the module without internal linkage will have edges
22/// from this external node, indicating that they could be called by
23/// functions outside of the module.
24/// 2. All functions whose address is used for something more than a direct
25/// call, for example being stored into a memory location will also have
26/// an edge from this external node. Since they may be called by an
27/// unknown caller later, they must be tracked as such.
28///
29/// There is a second external node added for calls that leave this module.
30/// Functions have a call edge to the external node iff:
31/// 1. The function is external, reflecting the fact that they could call
32/// anything without internal linkage or that has its address taken.
33/// 2. The function contains an indirect function call.
34///
35/// As an extension in the future, there may be multiple nodes with a null
36/// function. These will be used when we can prove (through pointer analysis)
37/// that an indirect call site can call only a specific set of functions.
38///
39/// Because of these properties, the CallGraph captures a conservative superset
40/// of all of the caller-callee relationships, which is useful for
41/// transformations.
42///
43//===----------------------------------------------------------------------===//
44
45#ifndef LLVM_ANALYSIS_CALLGRAPH_H
46#define LLVM_ANALYSIS_CALLGRAPH_H
47
48#include "llvm/ADT/GraphTraits.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/IR/CallSite.h"
51#include "llvm/IR/Function.h"
52#include "llvm/IR/Intrinsics.h"
53#include "llvm/IR/PassManager.h"
54#include "llvm/IR/ValueHandle.h"
55#include "llvm/Pass.h"
56#include <cassert>
57#include <map>
58#include <memory>
59#include <utility>
60#include <vector>
61
62namespace llvm {
63
64class CallGraphNode;
65class Module;
66class raw_ostream;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068/// The basic data container for the call graph of a \c Module of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069///
70/// This class exposes both the interface to the call graph for a module of IR.
71///
72/// The core call graph itself can also be updated to reflect changes to the IR.
73class CallGraph {
74 Module &M;
75
76 using FunctionMapTy =
77 std::map<const Function *, std::unique_ptr<CallGraphNode>>;
78
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 /// A map from \c Function* to \c CallGraphNode*.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080 FunctionMapTy FunctionMap;
81
Andrew Scullcdfcccc2018-10-05 20:58:37 +010082 /// This node has edges to all external functions and those internal
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083 /// functions that have their address taken.
84 CallGraphNode *ExternalCallingNode;
85
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 /// This node has edges to it from all functions making indirect calls
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 /// or calling an external function.
88 std::unique_ptr<CallGraphNode> CallsExternalNode;
89
Andrew Scullcdfcccc2018-10-05 20:58:37 +010090 /// Replace the function represented by this node by another.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 ///
92 /// This does not rescan the body of the function, so it is suitable when
93 /// splicing the body of one function to another while also updating all
94 /// callers from the old function to the new.
95 void spliceFunction(const Function *From, const Function *To);
96
Andrew Scullcdfcccc2018-10-05 20:58:37 +010097 /// Add a function to the call graph, and link the node to all of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098 /// functions that it calls.
99 void addToCallGraph(Function *F);
100
101public:
102 explicit CallGraph(Module &M);
103 CallGraph(CallGraph &&Arg);
104 ~CallGraph();
105
106 void print(raw_ostream &OS) const;
107 void dump() const;
108
109 using iterator = FunctionMapTy::iterator;
110 using const_iterator = FunctionMapTy::const_iterator;
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Returns the module the call graph corresponds to.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 Module &getModule() const { return M; }
114
115 inline iterator begin() { return FunctionMap.begin(); }
116 inline iterator end() { return FunctionMap.end(); }
117 inline const_iterator begin() const { return FunctionMap.begin(); }
118 inline const_iterator end() const { return FunctionMap.end(); }
119
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100120 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121 inline const CallGraphNode *operator[](const Function *F) const {
122 const_iterator I = FunctionMap.find(F);
123 assert(I != FunctionMap.end() && "Function not in callgraph!");
124 return I->second.get();
125 }
126
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100127 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 inline CallGraphNode *operator[](const Function *F) {
129 const_iterator I = FunctionMap.find(F);
130 assert(I != FunctionMap.end() && "Function not in callgraph!");
131 return I->second.get();
132 }
133
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100134 /// Returns the \c CallGraphNode which is used to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135 /// undetermined calls into the callgraph.
136 CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
137
138 CallGraphNode *getCallsExternalNode() const {
139 return CallsExternalNode.get();
140 }
141
142 //===---------------------------------------------------------------------
143 // Functions to keep a call graph up to date with a function that has been
144 // modified.
145 //
146
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100147 /// Unlink the function from this module, returning it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100148 ///
149 /// Because this removes the function from the module, the call graph node is
150 /// destroyed. This is only valid if the function does not call any other
151 /// functions (ie, there are no edges in it's CGN). The easiest way to do
152 /// this is to dropAllReferences before calling this.
153 Function *removeFunctionFromModule(CallGraphNode *CGN);
154
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100155 /// Similar to operator[], but this will insert a new CallGraphNode for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 /// \c F if one does not already exist.
157 CallGraphNode *getOrInsertFunction(const Function *F);
158};
159
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100160/// A node in the call graph for a module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161///
162/// Typically represents a function in the call graph. There are also special
163/// "null" nodes used to represent theoretical entries in the call graph.
164class CallGraphNode {
165public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100166 /// A pair of the calling instruction (a call or invoke)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167 /// and the call graph node being called.
168 using CallRecord = std::pair<WeakTrackingVH, CallGraphNode *>;
169
170public:
171 using CalledFunctionsVector = std::vector<CallRecord>;
172
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100173 /// Creates a node for the specified function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 inline CallGraphNode(Function *F) : F(F) {}
175
176 CallGraphNode(const CallGraphNode &) = delete;
177 CallGraphNode &operator=(const CallGraphNode &) = delete;
178
179 ~CallGraphNode() {
180 assert(NumReferences == 0 && "Node deleted while references remain");
181 }
182
183 using iterator = std::vector<CallRecord>::iterator;
184 using const_iterator = std::vector<CallRecord>::const_iterator;
185
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100186 /// Returns the function that this call graph node represents.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 Function *getFunction() const { return F; }
188
189 inline iterator begin() { return CalledFunctions.begin(); }
190 inline iterator end() { return CalledFunctions.end(); }
191 inline const_iterator begin() const { return CalledFunctions.begin(); }
192 inline const_iterator end() const { return CalledFunctions.end(); }
193 inline bool empty() const { return CalledFunctions.empty(); }
194 inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
195
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100196 /// Returns the number of other CallGraphNodes in this CallGraph that
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100197 /// reference this node in their callee list.
198 unsigned getNumReferences() const { return NumReferences; }
199
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100200 /// Returns the i'th called function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100201 CallGraphNode *operator[](unsigned i) const {
202 assert(i < CalledFunctions.size() && "Invalid index");
203 return CalledFunctions[i].second;
204 }
205
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100206 /// Print out this call graph node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100207 void dump() const;
208 void print(raw_ostream &OS) const;
209
210 //===---------------------------------------------------------------------
211 // Methods to keep a call graph up to date with a function that has been
212 // modified
213 //
214
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100215 /// Removes all edges from this CallGraphNode to any functions it
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216 /// calls.
217 void removeAllCalledFunctions() {
218 while (!CalledFunctions.empty()) {
219 CalledFunctions.back().second->DropRef();
220 CalledFunctions.pop_back();
221 }
222 }
223
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100224 /// Moves all the callee information from N to this node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100225 void stealCalledFunctionsFrom(CallGraphNode *N) {
226 assert(CalledFunctions.empty() &&
227 "Cannot steal callsite information if I already have some");
228 std::swap(CalledFunctions, N->CalledFunctions);
229 }
230
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100231 /// Adds a function to the list of functions called by this one.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100232 void addCalledFunction(CallSite CS, CallGraphNode *M) {
233 assert(!CS.getInstruction() || !CS.getCalledFunction() ||
234 !CS.getCalledFunction()->isIntrinsic() ||
235 !Intrinsic::isLeaf(CS.getCalledFunction()->getIntrinsicID()));
236 CalledFunctions.emplace_back(CS.getInstruction(), M);
237 M->AddRef();
238 }
239
240 void removeCallEdge(iterator I) {
241 I->second->DropRef();
242 *I = CalledFunctions.back();
243 CalledFunctions.pop_back();
244 }
245
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100246 /// Removes the edge in the node for the specified call site.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100247 ///
248 /// Note that this method takes linear time, so it should be used sparingly.
249 void removeCallEdgeFor(CallSite CS);
250
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100251 /// Removes all call edges from this node to the specified callee
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100252 /// function.
253 ///
254 /// This takes more time to execute than removeCallEdgeTo, so it should not
255 /// be used unless necessary.
256 void removeAnyCallEdgeTo(CallGraphNode *Callee);
257
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100258 /// Removes one edge associated with a null callsite from this node to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100259 /// the specified callee function.
260 void removeOneAbstractEdgeTo(CallGraphNode *Callee);
261
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100262 /// Replaces the edge in the node for the specified call site with a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100263 /// new one.
264 ///
265 /// Note that this method takes linear time, so it should be used sparingly.
266 void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
267
268private:
269 friend class CallGraph;
270
271 Function *F;
272
273 std::vector<CallRecord> CalledFunctions;
274
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100275 /// The number of times that this CallGraphNode occurs in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100276 /// CalledFunctions array of this or other CallGraphNodes.
277 unsigned NumReferences = 0;
278
279 void DropRef() { --NumReferences; }
280 void AddRef() { ++NumReferences; }
281
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100282 /// A special function that should only be used by the CallGraph class.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100283 void allReferencesDropped() { NumReferences = 0; }
284};
285
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100286/// An analysis pass to compute the \c CallGraph for a \c Module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100287///
288/// This class implements the concept of an analysis pass used by the \c
289/// ModuleAnalysisManager to run an analysis over a module and cache the
290/// resulting data.
291class CallGraphAnalysis : public AnalysisInfoMixin<CallGraphAnalysis> {
292 friend AnalysisInfoMixin<CallGraphAnalysis>;
293
294 static AnalysisKey Key;
295
296public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100297 /// A formulaic type to inform clients of the result type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298 using Result = CallGraph;
299
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100300 /// Compute the \c CallGraph for the module \c M.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100301 ///
302 /// The real work here is done in the \c CallGraph constructor.
303 CallGraph run(Module &M, ModuleAnalysisManager &) { return CallGraph(M); }
304};
305
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100306/// Printer pass for the \c CallGraphAnalysis results.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100307class CallGraphPrinterPass : public PassInfoMixin<CallGraphPrinterPass> {
308 raw_ostream &OS;
309
310public:
311 explicit CallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
312
313 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
314};
315
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100316/// The \c ModulePass which wraps up a \c CallGraph and the logic to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100317/// build it.
318///
319/// This class exposes both the interface to the call graph container and the
320/// module pass which runs over a module of IR and produces the call graph. The
321/// call graph interface is entirelly a wrapper around a \c CallGraph object
322/// which is stored internally for each module.
323class CallGraphWrapperPass : public ModulePass {
324 std::unique_ptr<CallGraph> G;
325
326public:
327 static char ID; // Class identification, replacement for typeinfo
328
329 CallGraphWrapperPass();
330 ~CallGraphWrapperPass() override;
331
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100332 /// The internal \c CallGraph around which the rest of this interface
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100333 /// is wrapped.
334 const CallGraph &getCallGraph() const { return *G; }
335 CallGraph &getCallGraph() { return *G; }
336
337 using iterator = CallGraph::iterator;
338 using const_iterator = CallGraph::const_iterator;
339
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100340 /// Returns the module the call graph corresponds to.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100341 Module &getModule() const { return G->getModule(); }
342
343 inline iterator begin() { return G->begin(); }
344 inline iterator end() { return G->end(); }
345 inline const_iterator begin() const { return G->begin(); }
346 inline const_iterator end() const { return G->end(); }
347
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100348 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100349 inline const CallGraphNode *operator[](const Function *F) const {
350 return (*G)[F];
351 }
352
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100353 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100354 inline CallGraphNode *operator[](const Function *F) { return (*G)[F]; }
355
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100356 /// Returns the \c CallGraphNode which is used to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100357 /// undetermined calls into the callgraph.
358 CallGraphNode *getExternalCallingNode() const {
359 return G->getExternalCallingNode();
360 }
361
362 CallGraphNode *getCallsExternalNode() const {
363 return G->getCallsExternalNode();
364 }
365
366 //===---------------------------------------------------------------------
367 // Functions to keep a call graph up to date with a function that has been
368 // modified.
369 //
370
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100371 /// Unlink the function from this module, returning it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100372 ///
373 /// Because this removes the function from the module, the call graph node is
374 /// destroyed. This is only valid if the function does not call any other
375 /// functions (ie, there are no edges in it's CGN). The easiest way to do
376 /// this is to dropAllReferences before calling this.
377 Function *removeFunctionFromModule(CallGraphNode *CGN) {
378 return G->removeFunctionFromModule(CGN);
379 }
380
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100381 /// Similar to operator[], but this will insert a new CallGraphNode for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100382 /// \c F if one does not already exist.
383 CallGraphNode *getOrInsertFunction(const Function *F) {
384 return G->getOrInsertFunction(F);
385 }
386
387 //===---------------------------------------------------------------------
388 // Implementation of the ModulePass interface needed here.
389 //
390
391 void getAnalysisUsage(AnalysisUsage &AU) const override;
392 bool runOnModule(Module &M) override;
393 void releaseMemory() override;
394
395 void print(raw_ostream &o, const Module *) const override;
396 void dump() const;
397};
398
399//===----------------------------------------------------------------------===//
400// GraphTraits specializations for call graphs so that they can be treated as
401// graphs by the generic graph algorithms.
402//
403
404// Provide graph traits for tranversing call graphs using standard graph
405// traversals.
406template <> struct GraphTraits<CallGraphNode *> {
407 using NodeRef = CallGraphNode *;
408 using CGNPairTy = CallGraphNode::CallRecord;
409
410 static NodeRef getEntryNode(CallGraphNode *CGN) { return CGN; }
411 static CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
412
413 using ChildIteratorType =
414 mapped_iterator<CallGraphNode::iterator, decltype(&CGNGetValue)>;
415
416 static ChildIteratorType child_begin(NodeRef N) {
417 return ChildIteratorType(N->begin(), &CGNGetValue);
418 }
419
420 static ChildIteratorType child_end(NodeRef N) {
421 return ChildIteratorType(N->end(), &CGNGetValue);
422 }
423};
424
425template <> struct GraphTraits<const CallGraphNode *> {
426 using NodeRef = const CallGraphNode *;
427 using CGNPairTy = CallGraphNode::CallRecord;
428 using EdgeRef = const CallGraphNode::CallRecord &;
429
430 static NodeRef getEntryNode(const CallGraphNode *CGN) { return CGN; }
431 static const CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
432
433 using ChildIteratorType =
434 mapped_iterator<CallGraphNode::const_iterator, decltype(&CGNGetValue)>;
435 using ChildEdgeIteratorType = CallGraphNode::const_iterator;
436
437 static ChildIteratorType child_begin(NodeRef N) {
438 return ChildIteratorType(N->begin(), &CGNGetValue);
439 }
440
441 static ChildIteratorType child_end(NodeRef N) {
442 return ChildIteratorType(N->end(), &CGNGetValue);
443 }
444
445 static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
446 return N->begin();
447 }
448 static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
449
450 static NodeRef edge_dest(EdgeRef E) { return E.second; }
451};
452
453template <>
454struct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> {
455 using PairTy =
456 std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
457
458 static NodeRef getEntryNode(CallGraph *CGN) {
459 return CGN->getExternalCallingNode(); // Start at the external node!
460 }
461
462 static CallGraphNode *CGGetValuePtr(const PairTy &P) {
463 return P.second.get();
464 }
465
466 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
467 using nodes_iterator =
468 mapped_iterator<CallGraph::iterator, decltype(&CGGetValuePtr)>;
469
470 static nodes_iterator nodes_begin(CallGraph *CG) {
471 return nodes_iterator(CG->begin(), &CGGetValuePtr);
472 }
473
474 static nodes_iterator nodes_end(CallGraph *CG) {
475 return nodes_iterator(CG->end(), &CGGetValuePtr);
476 }
477};
478
479template <>
480struct GraphTraits<const CallGraph *> : public GraphTraits<
481 const CallGraphNode *> {
482 using PairTy =
483 std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
484
485 static NodeRef getEntryNode(const CallGraph *CGN) {
486 return CGN->getExternalCallingNode(); // Start at the external node!
487 }
488
489 static const CallGraphNode *CGGetValuePtr(const PairTy &P) {
490 return P.second.get();
491 }
492
493 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
494 using nodes_iterator =
495 mapped_iterator<CallGraph::const_iterator, decltype(&CGGetValuePtr)>;
496
497 static nodes_iterator nodes_begin(const CallGraph *CG) {
498 return nodes_iterator(CG->begin(), &CGGetValuePtr);
499 }
500
501 static nodes_iterator nodes_end(const CallGraph *CG) {
502 return nodes_iterator(CG->end(), &CGGetValuePtr);
503 }
504};
505
506} // end namespace llvm
507
508#endif // LLVM_ANALYSIS_CALLGRAPH_H