llvm.org GIT mirror llvm / cee8f9a
Create a new #include "Support/..." directory structure to move things from "llvm/Support/..." that are not llvm dependant. Move files and fix #includes git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1400 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 18 years ago
99 changed file(s) with 2548 addition(s) and 1014 deletion(s). Raw diff Collapse all Expand all
6363 To start out, you need to include the CommandLine header file into your program:

6464
6565

                  
                
66 #include "llvm/Support/CommandLine.h"
66 #include "Support/CommandLine.h"
6767

6868
6969 Additionally, you need to add this as the first line of your main program:

352352
Chris Lattner
353353
354354
355 Last modified: Mon Jul 23 17:33:57 CDT 2001
355 Last modified: Mon Nov 26 17:09:39 CST 2001
356356
357357
358358
0 //===- Support/CommandLine.h - Flexible Command line parser ------*- C++ -*--=//
1 //
2 // This class implements a command line argument processor that is useful when
3 // creating a tool. It provides a simple, minimalistic interface that is easily
4 // extensible and supports nonlocal (library) command line options.
5 //
6 // Note that rather than trying to figure out what this code does, you could try
7 // reading the library documentation located in docs/CommandLine.html
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_SUPPORT_COMMANDLINE_H
12 #define LLVM_SUPPORT_COMMANDLINE_H
13
14 #include
15 #include
16 #include
17 #include
18
19 namespace cl { // Short namespace to make usage concise
20
21 //===----------------------------------------------------------------------===//
22 // ParseCommandLineOptions - Minimalistic command line option processing entry
23 //
24 void cl::ParseCommandLineOptions(int &argc, char **argv,
25 const char *Overview = 0,
26 int Flags = 0);
27
28 // ParserOptions - This set of option is use to control global behavior of the
29 // command line processor.
30 //
31 enum ParserOptions {
32 // DisableSingleLetterArgGrouping - With this option enabled, multiple letter
33 // options are allowed to bunch together with only a single hyphen for the
34 // whole group. This allows emulation of the behavior that ls uses for
35 // example: ls -la === ls -l -a Providing this option, disables this.
36 //
37 DisableSingleLetterArgGrouping = 0x0001,
38
39 // EnableSingleLetterArgValue - This option allows arguments that are
40 // otherwise unrecognized to match single letter flags that take a value.
41 // This is useful for cases like a linker, where options are typically of the
42 // form '-lfoo' or '-L../../include' where -l or -L are the actual flags.
43 //
44 EnableSingleLetterArgValue = 0x0002,
45 };
46
47
48 //===----------------------------------------------------------------------===//
49 // Global flags permitted to be passed to command line arguments
50
51 enum FlagsOptions {
52 NoFlags = 0x00, // Marker to make explicit that we have no flags
53 Default = 0x00, // Equally, marker to use the default flags
54
55 GlobalsMask = 0x80,
56 };
57
58 enum NumOccurances { // Flags for the number of occurances allowed...
59 Optional = 0x01, // Zero or One occurance
60 ZeroOrMore = 0x02, // Zero or more occurances allowed
61 Required = 0x03, // One occurance required
62 OneOrMore = 0x04, // One or more occurances required
63
64 // ConsumeAfter - Marker for a null ("") flag that can be used to indicate
65 // that anything that matches the null marker starts a sequence of options
66 // that all get sent to the null marker. Thus, for example, all arguments
67 // to LLI are processed until a filename is found. Once a filename is found,
68 // all of the succeeding arguments are passed, unprocessed, to the null flag.
69 //
70 ConsumeAfter = 0x05,
71
72 OccurancesMask = 0x07,
73 };
74
75 enum ValueExpected { // Is a value required for the option?
76 ValueOptional = 0x08, // The value can oppear... or not
77 ValueRequired = 0x10, // The value is required to appear!
78 ValueDisallowed = 0x18, // A value may not be specified (for flags)
79 ValueMask = 0x18,
80 };
81
82 enum OptionHidden { // Control whether -help shows this option
83 NotHidden = 0x20, // Option included in --help & --help-hidden
84 Hidden = 0x40, // -help doesn't, but --help-hidden does
85 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
86 HiddenMask = 0x60,
87 };
88
89
90 //===----------------------------------------------------------------------===//
91 // Option Base class
92 //
93 class Alias;
94 class Option {
95 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
96 friend class Alias;
97
98 // handleOccurances - Overriden by subclasses to handle the value passed into
99 // an argument. Should return true if there was an error processing the
100 // argument and the program should exit.
101 //
102 virtual bool handleOccurance(const char *ArgName, const string &Arg) = 0;
103
104 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
105 return Optional;
106 }
107 virtual enum ValueExpected getValueExpectedFlagDefault() const {
108 return ValueOptional;
109 }
110 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
111 return NotHidden;
112 }
113
114 int NumOccurances; // The number of times specified
115 const int Flags; // Flags for the argument
116 public:
117 const char * const ArgStr; // The argument string itself (ex: "help", "o")
118 const char * const HelpStr; // The descriptive text message for --help
119
120 inline enum NumOccurances getNumOccurancesFlag() const {
121 int NO = Flags & OccurancesMask;
122 return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
123 }
124 inline enum ValueExpected getValueExpectedFlag() const {
125 int VE = Flags & ValueMask;
126 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
127 }
128 inline enum OptionHidden getOptionHiddenFlag() const {
129 int OH = Flags & HiddenMask;
130 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
131 }
132
133 protected:
134 Option(const char *ArgStr, const char *Message, int Flags);
135 Option(int flags) : NumOccurances(0), Flags(flags), ArgStr(""), HelpStr("") {}
136
137 public:
138 // Return the width of the option tag for printing...
139 virtual unsigned getOptionWidth() const;
140
141 // printOptionInfo - Print out information about this option. The
142 // to-be-maintained width is specified.
143 //
144 virtual void printOptionInfo(unsigned GlobalWidth) const;
145
146 // addOccurance - Wrapper around handleOccurance that enforces Flags
147 //
148 bool addOccurance(const char *ArgName, const string &Value);
149
150 // Prints option name followed by message. Always returns true.
151 bool error(string Message, const char *ArgName = 0);
152
153 public:
154 inline int getNumOccurances() const { return NumOccurances; }
155 virtual ~Option() {}
156 };
157
158
159 //===----------------------------------------------------------------------===//
160 // Aliased command line option (alias this name to a preexisting name)
161 //
162 class Alias : public Option {
163 Option &AliasFor;
164 virtual bool handleOccurance(const char *ArgName, const string &Arg) {
165 return AliasFor.handleOccurance(AliasFor.ArgStr, Arg);
166 }
167 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
168 public:
169 inline Alias(const char *ArgStr, const char *Message, int Flags,
170 Option &aliasFor) : Option(ArgStr, Message, Flags),
171 AliasFor(aliasFor) {}
172 };
173
174 //===----------------------------------------------------------------------===//
175 // Boolean/flag command line option
176 //
177 class Flag : public Option {
178 bool Value;
179 virtual bool handleOccurance(const char *ArgName, const string &Arg);
180 public:
181 inline Flag(const char *ArgStr, const char *Message, int Flags = 0,
182 bool DefaultVal = 0) : Option(ArgStr, Message, Flags),
183 Value(DefaultVal) {}
184 operator const bool() const { return Value; }
185 inline bool operator=(bool Val) { Value = Val; return Val; }
186 };
187
188
189
190 //===----------------------------------------------------------------------===//
191 // Integer valued command line option
192 //
193 class Int : public Option {
194 int Value;
195 virtual bool handleOccurance(const char *ArgName, const string &Arg);
196 virtual enum ValueExpected getValueExpectedFlagDefault() const {
197 return ValueRequired;
198 }
199 public:
200 inline Int(const char *ArgStr, const char *Help, int Flags = 0,
201 int DefaultVal = 0) : Option(ArgStr, Help, Flags),
202 Value(DefaultVal) {}
203 inline operator int() const { return Value; }
204 inline int operator=(int Val) { Value = Val; return Val; }
205 };
206
207
208 //===----------------------------------------------------------------------===//
209 // String valued command line option
210 //
211 class String : public Option, public string {
212 virtual bool handleOccurance(const char *ArgName, const string &Arg);
213 virtual enum ValueExpected getValueExpectedFlagDefault() const {
214 return ValueRequired;
215 }
216 public:
217 inline String(const char *ArgStr, const char *Help, int Flags = 0,
218 const char *DefaultVal = "")
219 : Option(ArgStr, Help, Flags), string(DefaultVal) {}
220
221 inline const string &operator=(const string &Val) {
222 return string::operator=(Val);
223 }
224 };
225
226
227 //===----------------------------------------------------------------------===//
228 // String list command line option
229 //
230 class StringList : public Option, public vector {
231
232 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
233 return ZeroOrMore;
234 }
235 virtual enum ValueExpected getValueExpectedFlagDefault() const {
236 return ValueRequired;
237 }
238 virtual bool handleOccurance(const char *ArgName, const string &Arg);
239
240 public:
241 inline StringList(const char *ArgStr, const char *Help, int Flags = 0)
242 : Option(ArgStr, Help, Flags) {}
243 };
244
245
246 //===----------------------------------------------------------------------===//
247 // Enum valued command line option
248 //
249 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, ENUMVAL, DESC
250 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, ENUMVAL, DESC
251
252 // EnumBase - Base class for all enum/varargs related argument types...
253 class EnumBase : public Option {
254 protected:
255 // Use a vector instead of a map, because the lists should be short,
256 // the overhead is less, and most importantly, it keeps them in the order
257 // inserted so we can print our option out nicely.
258 vector > > ValueMap;
259
260 inline EnumBase(const char *ArgStr, const char *Help, int Flags)
261 : Option(ArgStr, Help, Flags) {}
262 inline EnumBase(int Flags) : Option(Flags) {}
263
264 // processValues - Incorporate the specifed varargs arglist into the
265 // ValueMap.
266 //
267 void processValues(va_list Vals);
268
269 // registerArgs - notify the system about these new arguments
270 void registerArgs();
271
272 public:
273 // Turn an enum into the arg name that activates it
274 const char *getArgName(int ID) const;
275 const char *getArgDescription(int ID) const;
276 };
277
278 class EnumValueBase : public EnumBase {
279 protected:
280 int Value;
281 inline EnumValueBase(const char *ArgStr, const char *Help, int Flags)
282 : EnumBase(ArgStr, Help, Flags) {}
283 inline EnumValueBase(int Flags) : EnumBase(Flags) {}
284
285 // handleOccurance - Set Value to the enum value specified by Arg
286 virtual bool handleOccurance(const char *ArgName, const string &Arg);
287
288 // Return the width of the option tag for printing...
289 virtual unsigned getOptionWidth() const;
290
291 // printOptionInfo - Print out information about this option. The
292 // to-be-maintained width is specified.
293 //
294 virtual void printOptionInfo(unsigned GlobalWidth) const;
295 };
296
297 template // The enum we are representing
298 class Enum : public EnumValueBase {
299 virtual enum ValueExpected getValueExpectedFlagDefault() const {
300 return ValueRequired;
301 }
302 public:
303 inline Enum(const char *ArgStr, int Flags, const char *Help, ...)
304 : EnumValueBase(ArgStr, Help, Flags) {
305 va_list Values;
306 va_start(Values, Help);
307 processValues(Values);
308 va_end(Values);
309 Value = ValueMap.front().second.first; // Grab default value
310 }
311
312 inline operator E() const { return (E)Value; }
313 inline E operator=(E Val) { Value = Val; return Val; }
314 };
315
316
317 //===----------------------------------------------------------------------===//
318 // Enum flags command line option
319 //
320 class EnumFlagsBase : public EnumValueBase {
321 virtual enum ValueExpected getValueExpectedFlagDefault() const {
322 return ValueDisallowed;
323 }
324 protected:
325 virtual bool handleOccurance(const char *ArgName, const string &Arg);
326 inline EnumFlagsBase(int Flags) : EnumValueBase(Flags) {}
327
328 // Return the width of the option tag for printing...
329 virtual unsigned getOptionWidth() const;
330
331 // printOptionInfo - Print out information about this option. The
332 // to-be-maintained width is specified.
333 //
334 virtual void printOptionInfo(unsigned GlobalWidth) const;
335 };
336
337 template // The enum we are representing
338 class EnumFlags : public EnumFlagsBase {
339 public:
340 inline EnumFlags(int Flags, ...) : EnumFlagsBase(Flags) {
341 va_list Values;
342 va_start(Values, Flags);
343 processValues(Values);
344 va_end(Values);
345 registerArgs();
346 Value = ValueMap.front().second.first; // Grab default value
347 }
348
349 inline operator E() const { return (E)Value; }
350 inline E operator=(E Val) { Value = Val; return Val; }
351 };
352
353
354 //===----------------------------------------------------------------------===//
355 // Enum list command line option
356 //
357 class EnumListBase : public EnumBase {
358 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
359 return ZeroOrMore;
360 }
361 virtual enum ValueExpected getValueExpectedFlagDefault() const {
362 return ValueDisallowed;
363 }
364 protected:
365 vector Values; // The options specified so far.
366
367 inline EnumListBase(int Flags)
368 : EnumBase(Flags) {}
369 virtual bool handleOccurance(const char *ArgName, const string &Arg);
370
371 // Return the width of the option tag for printing...
372 virtual unsigned getOptionWidth() const;
373
374 // printOptionInfo - Print out information about this option. The
375 // to-be-maintained width is specified.
376 //
377 virtual void printOptionInfo(unsigned GlobalWidth) const;
378 public:
379 inline unsigned size() { return Values.size(); }
380 };
381
382 template // The enum we are representing
383 class EnumList : public EnumListBase {
384 public:
385 inline EnumList(int Flags, ...) : EnumListBase(Flags) {
386 va_list Values;
387 va_start(Values, Flags);
388 processValues(Values);
389 va_end(Values);
390 registerArgs();
391 }
392 inline E operator[](unsigned i) const { return (E)Values[i]; }
393 inline E &operator[](unsigned i) { return (E&)Values[i]; }
394 };
395
396 } // End namespace cl
397
398 #endif
0 //===- Support/DepthFirstIterator.h - Depth First iterator -------*- C++ -*--=//
1 //
2 // This file builds on the Support/GraphTraits.h file to build generic depth
3 // first graph iterator.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
8 #define LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
9
10 #include "Support/GraphTraits.h"
11 #include
12 #include
13 #include
14
15 // Generic Depth First Iterator
16 template >
17 class df_iterator : public std::forward_iterator
18 ptrdiff_t> {
19 typedef typename GT::NodeType NodeType;
20 typedef typename GT::ChildIteratorType ChildItTy;
21
22 set Visited; // All of the blocks visited so far...
23 // VisitStack - Used to maintain the ordering. Top = current block
24 // First element is node pointer, second is the 'next child' to visit
25 stack > VisitStack;
26 const bool Reverse; // Iterate over children before self?
27 private:
28 void reverseEnterNode() {
29 pair &Top = VisitStack.top();
30 NodeType *Node = Top.first;
31 ChildItTy &It = Top.second;
32 for (; It != GT::child_end(Node); ++It) {
33 NodeType *Child = *It;
34 if (!Visited.count(Child)) {
35 Visited.insert(Child);
36 VisitStack.push(make_pair(Child, GT::child_begin(Child)));
37 reverseEnterNode();
38 return;
39 }
40 }
41 }
42
43 inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) {
44 Visited.insert(Node);
45 VisitStack.push(make_pair(Node, GT::child_begin(Node)));
46 if (Reverse) reverseEnterNode();
47 }
48 inline df_iterator() { /* End is when stack is empty */ }
49
50 public:
51 typedef df_iterator _Self;
52
53 // Provide static begin and end methods as our public "constructors"
54 static inline _Self begin(GraphT G, bool Reverse = false) {
55 return _Self(GT::getEntryNode(G), Reverse);
56 }
57 static inline _Self end(GraphT G) { return _Self(); }
58
59
60 inline bool operator==(const _Self& x) const {
61 return VisitStack == x.VisitStack;
62 }
63 inline bool operator!=(const _Self& x) const { return !operator==(x); }
64
65 inline pointer operator*() const {
66 return VisitStack.top().first;
67 }
68
69 // This is a nonstandard operator-> that dereferences the pointer an extra
70 // time... so that you can actually call methods ON the Node, because
71 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
72 //
73 inline NodeType *operator->() const { return operator*(); }
74
75 inline _Self& operator++() { // Preincrement
76 if (Reverse) { // Reverse Depth First Iterator
77 if (VisitStack.top().second == GT::child_end(VisitStack.top().first))
78 VisitStack.pop();
79 if (!VisitStack.empty())
80 reverseEnterNode();
81 } else { // Normal Depth First Iterator
82 do {
83 pair &Top = VisitStack.top();
84 NodeType *Node = Top.first;
85 ChildItTy &It = Top.second;
86
87 while (It != GT::child_end(Node)) {
88 NodeType *Next = *It++;
89 if (!Visited.count(Next)) { // Has our next sibling been visited?
90 // No, do it now.
91 Visited.insert(Next);
92 VisitStack.push(make_pair(Next, GT::child_begin(Next)));
93 return *this;
94 }
95 }
96
97 // Oops, ran out of successors... go up a level on the stack.
98 VisitStack.pop();
99 } while (!VisitStack.empty());
100 }
101 return *this;
102 }
103
104 inline _Self operator++(int) { // Postincrement
105 _Self tmp = *this; ++*this; return tmp;
106 }
107
108 // nodeVisited - return true if this iterator has already visited the
109 // specified node. This is public, and will probably be used to iterate over
110 // nodes that a depth first iteration did not find: ie unreachable nodes.
111 //
112 inline bool nodeVisited(NodeType *Node) const {
113 return Visited.count(Node) != 0;
114 }
115 };
116
117
118 // Provide global constructors that automatically figure out correct types...
119 //
120 template
121 df_iterator df_begin(T G, bool Reverse = false) {
122 return df_iterator::begin(G, Reverse);
123 }
124
125 template
126 df_iterator df_end(T G) {
127 return df_iterator::end(G);
128 }
129
130 // Provide global definitions of inverse depth first iterators...
131 template
132 struct idf_iterator : public df_iterator > {
133 idf_iterator(const df_iterator > &V) :df_iterator >(V){}
134 };
135
136 template
137 idf_iterator idf_begin(T G, bool Reverse = false) {
138 return idf_iterator::begin(G, Reverse);
139 }
140
141 template
142 idf_iterator idf_end(T G){
143 return idf_iterator::end(G);
144 }
145
146 #endif
0 //===-- Support/GraphTraits.h - Graph traits template ------------*- C++ -*--=//
1 //
2 // This file defines the little GraphTraits template class that should be
3 // specialized by classes that want to be iteratable by generic graph iterators.
4 //
5 // This file also defines the marker class Inverse that is used to iterate over
6 // graphs in a graph defined, inverse ordering...
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_GRAPH_TRAITS_H
11 #define LLVM_SUPPORT_GRAPH_TRAITS_H
12
13 // GraphTraits - This class should be specialized by different graph types...
14 // which is why the default version is empty.
15 //
16 template
17 struct GraphTraits {
18 // Elements to provide:
19
20 // typedef NodeType - Type of Node in the graph
21 // typedef ChildIteratorType - Type used to iterate over children in graph
22
23 // static NodeType *getEntryNode(GraphType *)
24 // Return the entry node of the graph
25
26 // static ChildIteratorType child_begin(NodeType *)
27 // static ChildIteratorType child_end (NodeType *)
28 // Return iterators that point to the beginning and ending of the child
29 // node list for the specified node.
30 //
31
32
33 // If anyone tries to use this class without having an appropriate
34 // specialization, make an error. If you get this error, it's because you
35 // need to include the appropriate specialization of GraphTraits<> for your
36 // graph, or you need to define it for a new graph type. Either that or
37 // your argument to XXX_begin(...) is unknown or needs to have the proper .h
38 // file #include'd.
39 //
40 typedef typename GraphType::UnknownGraphTypeError NodeType;
41 };
42
43
44 // Inverse - This class is used as a little marker class to tell the graph
45 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
46 // Not all graphs define an inverse ordering, and if they do, it depends on
47 // the graph exactly what that is. Here's an example of usage with the
48 // df_iterator:
49 //
50 // idf_iterator I = idf_begin(M), E = idf_end(M);
51 // for (; I != E; ++I) { ... }
52 //
53 // Which is equivalent to:
54 // df_iterator > I = idf_begin(M), E = idf_end(M);
55 // for (; I != E; ++I) { ... }
56 //
57 template
58 struct Inverse {
59 GraphType &Graph;
60
61 inline Inverse(GraphType &G) : Graph(G) {}
62 };
63
64 #endif
0 //===-- HashExtras.h - Useful functions for STL hash containers --*- C++ -*--=//
1 //
2 // This file contains some templates that are useful if you are working with the
3 // STL Hashed containers.
4 //
5 // No library is required when using these functinons.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_HASHEXTRAS_H
10 #define LLVM_SUPPORT_HASHEXTRAS_H
11
12 #include
13 #include
14
15 template <> struct hash {
16 size_t operator()(string const &str) const {
17 return hash()(str.c_str());
18 }
19 };
20
21 // Provide a hash function for arbitrary pointers...
22 template struct hash {
23 inline size_t operator()(const T *Val) const { return (size_t)Val; }
24 };
25
26 #endif
0 // $Id$ -*-c++-*-
1 //***************************************************************************
2 // File:
3 // MathExtras.h
4 //
5 // Purpose:
6 //
7 // History:
8 // 8/25/01 - Vikram Adve - Created
9 //**************************************************************************/
10
11 #ifndef LLVM_SUPPORT_MATH_EXTRAS_H
12 #define LLVM_SUPPORT_MATH_EXTRAS_H
13
14 #include
15
16 inline bool IsPowerOf2 (int64_t C, unsigned& getPow);
17
18 inline
19 bool IsPowerOf2(int64_t C, unsigned& getPow)
20 {
21 if (C < 0)
22 C = -C;
23 bool isBool = C > 0 && (C == (C & ~(C - 1)));
24 if (isBool)
25 for (getPow = 0; C > 1; getPow++)
26 C = C >> 1;
27
28 return isBool;
29 }
30
31 #endif /*LLVM_SUPPORT_MATH_EXTRAS_H*/
0 //===-- NonCopyable.h - Disable copy ctor and op= in subclasses --*- C++ -*--=//
1 //
2 // This file defines the NonCopyable and NonCopyableV classes. These mixin
3 // classes may be used to mark a class not being copyable. You should derive
4 // from NonCopyable if you don't want to have a virtual dtor, or NonCopyableV
5 // if you do want polymorphic behavior in your class.
6 //
7 // No library is required when using these functinons.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_SUPPORT_NONCOPYABLE_H
12 #define LLVM_SUPPORT_NONCOPYABLE_H
13
14 class NonCopyable {
15 // Disable the copy constructor and the assignment operator
16 // by making them both private:
17 //
18 NonCopyable(const NonCopyable &); // DO NOT IMPLEMENT
19 NonCopyable &operator=(const NonCopyable &); // DO NOT IMPLEMENT
20 protected:
21 inline NonCopyable() {}
22 inline ~NonCopyable() {}
23 };
24
25 class NonCopyableV {
26 // Disable the copy constructor and the assignment operator
27 // by making them both private:
28 //
29 NonCopyableV(const NonCopyableV &); // DO NOT IMPLEMENT
30 NonCopyableV &operator=(const NonCopyableV &); // DO NOT IMPLEMENT
31 protected:
32 inline NonCopyableV() {}
33 virtual ~NonCopyableV() {}
34 };
35
36 #endif
0 //===-- Support/PostOrderIterator.h - Generic PostOrder iterator -*- C++ -*--=//
1 //
2 // This file builds on the Support/GraphTraits.h file to build a generic graph
3 // post order iterator. This should work over any graph type that has a
4 // GraphTraits specialization.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_SUPPORT_POSTORDER_ITERATOR_H
9 #define LLVM_SUPPORT_POSTORDER_ITERATOR_H
10
11 #include "Support/GraphTraits.h"
12 #include
13 #include
14 #include
15
16 template >
17 class po_iterator : public std::forward_iterator
18 ptrdiff_t> {
19 typedef typename GT::NodeType NodeType;
20 typedef typename GT::ChildIteratorType ChildItTy;
21
22 set Visited; // All of the blocks visited so far...
23 // VisitStack - Used to maintain the ordering. Top = current block
24 // First element is basic block pointer, second is the 'next child' to visit
25 stack > VisitStack;
26
27 void traverseChild() {
28 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
29 NodeType *BB = *VisitStack.top().second++;
30 if (!Visited.count(BB)) { // If the block is not visited...
31 Visited.insert(BB);
32 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
33 }
34 }
35 }
36
37 inline po_iterator(NodeType *BB) {
38 Visited.insert(BB);
39 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
40 traverseChild();
41 }
42 inline po_iterator() { /* End is when stack is empty */ }
43 public:
44 typedef po_iterator _Self;
45
46 // Provide static "constructors"...
47 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
48 static inline _Self end (GraphT G) { return _Self(); }
49
50 inline bool operator==(const _Self& x) const {
51 return VisitStack == x.VisitStack;
52 }
53 inline bool operator!=(const _Self& x) const { return !operator==(x); }
54
55 inline pointer operator*() const {
56 return VisitStack.top().first;
57 }
58
59 // This is a nonstandard operator-> that dereferences the pointer an extra
60 // time... so that you can actually call methods ON the BasicBlock, because
61 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
62 //
63 inline NodeType *operator->() const { return operator*(); }
64
65 inline _Self& operator++() { // Preincrement
66 VisitStack.pop();
67 if (!VisitStack.empty())
68 traverseChild();
69 return *this;
70 }
71
72 inline _Self operator++(int) { // Postincrement
73 _Self tmp = *this; ++*this; return tmp;
74 }
75 };
76
77 // Provide global constructors that automatically figure out correct types...
78 //
79 template
80 po_iterator po_begin(T G) { return po_iterator::begin(G); }
81 template
82 po_iterator po_end (T G) { return po_iterator::end(G); }
83
84 // Provide global definitions of inverse post order iterators...
85 template
86 struct ipo_iterator : public po_iterator > {
87 ipo_iterator(const po_iterator > &V) :po_iterator >(V){}
88 };
89
90 template
91 ipo_iterator ipo_begin(T G, bool Reverse = false) {
92 return ipo_iterator::begin(G, Reverse);
93 }
94
95 template
96 ipo_iterator ipo_end(T G){
97 return ipo_iterator::end(G);
98 }
99
100
101 //===--------------------------------------------------------------------===//
102 // Reverse Post Order CFG iterator code
103 //===--------------------------------------------------------------------===//
104 //
105 // This is used to visit basic blocks in a method in reverse post order. This
106 // class is awkward to use because I don't know a good incremental algorithm to
107 // computer RPO from a graph. Because of this, the construction of the
108 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
109 // with a postorder iterator to build the data structures). The moral of this
110 // story is: Don't create more ReversePostOrderTraversal classes than neccesary.
111 //
112 // This class should be used like this:
113 // {
114 // cfg::ReversePostOrderTraversal RPOT(MethodPtr); // Expensive to create
115 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
116 // ...
117 // }
118 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
119 // ...
120 // }
121 // }
122 //
123
124 typedef reverse_iterator::iterator> rpo_iterator;
125 // TODO: FIXME: ReversePostOrderTraversal is not generic!
126 class ReversePostOrderTraversal {
127 vector Blocks; // Block list in normal PO order
128 inline void Initialize(BasicBlock *BB) {
129 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
130 }
131 public:
132 inline ReversePostOrderTraversal(Method *M) {
133 Initialize(M->front());
134 }
135 inline ReversePostOrderTraversal(BasicBlock *BB) {
136 Initialize(BB);
137 }
138
139 // Because we want a reverse post order, use reverse iterators from the vector
140 inline rpo_iterator begin() { return Blocks.rbegin(); }
141 inline rpo_iterator end() { return Blocks.rend(); }
142 };
143
144 #endif
0 //===-- STLExtras.h - Useful functions when working with the STL -*- C++ -*--=//
1 //
2 // This file contains some templates that are useful if you are working with the
3 // STL at all.
4 //
5 // No library is required when using these functinons.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_STL_EXTRAS_H
10 #define LLVM_SUPPORT_STL_EXTRAS_H
11
12 #include
13
14 //===----------------------------------------------------------------------===//
15 // Extra additions to
16 //===----------------------------------------------------------------------===//
17
18 // bind_obj - Often times you want to apply the member function of an object
19 // as a unary functor. This macro is shorthand that makes it happen less
20 // verbosely.
21 //
22 // Example:
23 // struct Summer { void accumulate(int x); }
24 // vector Numbers;
25 // Summer MyS;
26 // for_each(Numbers.begin(), Numbers.end(),
27 // bind_obj(&MyS, &Summer::accumulate));
28 //
29 // TODO: When I get lots of extra time, convert this from an evil macro
30 //
31 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
32
33
34 // bitwise_or - This is a simple functor that applys operator| on its two
35 // arguments to get a boolean result.
36 //
37 template
38 struct bitwise_or : public binary_function {
39 bool operator()(const Ty& left, const Ty& right) const {
40 return left | right;
41 }
42 };
43
44
45 // deleter - Very very very simple method that is used to invoke operator
46 // delete on something. It is used like this:
47 //
48 // for_each(V.begin(), B.end(), deleter);
49 //
50 template
51 static inline void deleter(T *Ptr) {
52 delete Ptr;
53 }
54
55
56
57 //===----------------------------------------------------------------------===//
58 // Extra additions to
59 //===----------------------------------------------------------------------===//
60
61 // mapped_iterator - This is a simple iterator adapter that causes a function to
62 // be dereferenced whenever operator* is invoked on the iterator.
63 //
64 // It turns out that this is disturbingly similar to boost::transform_iterator
65 //
66 #if 1
67 template
68 class mapped_iterator {
69 RootIt current;
70 UnaryFunc Fn;
71 public:
72 typedef typename iterator_traits::iterator_category
73 iterator_category;
74 typedef typename iterator_traits::difference_type
75 difference_type;
76 typedef typename UnaryFunc::result_type value_type;
77 typedef typename UnaryFunc::result_type *pointer;
78 typedef void reference; // Can't modify value returned by fn
79
80 typedef RootIt iterator_type;
81 typedef mapped_iterator _Self;
82
83 inline RootIt &getCurrent() const { return current; }
84
85 inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
86 : current(I), Fn(F) {}
87 inline mapped_iterator(const mapped_iterator &It)
88 : current(It.current), Fn(It.Fn) {}
89
90 inline value_type operator*() const { // All this work to do this
91 return Fn(*current); // little change
92 }
93
94 _Self& operator++() { ++current; return *this; }
95 _Self& operator--() { --current; return *this; }
96 _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
97 _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; }
98 _Self operator+ (difference_type n) const { return _Self(current + n); }
99 _Self& operator+= (difference_type n) { current += n; return *this; }
100 _Self operator- (difference_type n) const { return _Self(current - n); }
101 _Self& operator-= (difference_type n) { current -= n; return *this; }
102 reference operator[](difference_type n) const { return *(*this + n); }
103
104 inline bool operator==(const _Self &X) const { return current == X.current; }
105 inline bool operator< (const _Self &X) const { return current < X.current; }
106
107 inline difference_type operator-(const _Self &X) const {
108 return current - X.current;
109 }
110 };
111
112 template
113 inline mapped_iterator<_Iterator, Func>
114 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
115 const mapped_iterator<_Iterator, Func>& X) {
116 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
117 }
118
119 #else
120
121 // This fails to work, because some iterators are not classes, for example
122 // vector iterators are commonly value_type **'s
123 template
124 class mapped_iterator : public RootIt {
125 UnaryFunc Fn;
126 public:
127 typedef typename UnaryFunc::result_type value_type;
128 typedef typename UnaryFunc::result_type *pointer;
129 typedef void reference; // Can't modify value returned by fn
130
131 typedef mapped_iterator _Self;
132 typedef RootIt super;
133 inline explicit mapped_iterator(const RootIt &I) : super(I) {}
134 inline mapped_iterator(const super &It) : super(It) {}
135
136 inline value_type operator*() const { // All this work to do
137 return Fn(super::operator*()); // this little thing
138 }
139 };
140 #endif
141
142 // map_iterator - Provide a convenient way to create mapped_iterators, just like
143 // make_pair is useful for creating pairs...
144 //
145 template
146 inline mapped_iterator map_iterator(const ItTy &I, FuncTy F) {
147 return mapped_iterator(I, F);
148 }
149
150
151 //===----------------------------------------------------------------------===//
152 // Extra additions to
153 //===----------------------------------------------------------------------===//
154
155 // apply_until - Apply a functor to a sequence continually, unless the
156 // functor returns true. Return true if the functor returned true, return false
157 // if the functor never returned true.
158 //
159 template
160 bool apply_until(InputIt First, InputIt Last, Function Func) {
161 for ( ; First != Last; ++First)
162 if (Func(*First)) return true;
163 return false;
164 }
165
166
167 // reduce - Reduce a sequence values into a single value, given an initial
168 // value and an operator.
169 //
170 template
171 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
172 for ( ; First != Last; ++First)
173 Value = Func(*First, Value);
174 return Value;
175 }
176
177 #if 1 // This is likely to be more efficient
178
179 // reduce_apply - Reduce the result of applying a function to each value in a
180 // sequence, given an initial value, an operator, a function, and a sequence.
181 //
182 template
183 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func,
184 ValueType Value, TransFunc XForm) {
185 for ( ; First != Last; ++First)
186 Value = Func(XForm(*First), Value);
187 return Value;
188 }
189
190 #else // This is arguably more elegant
191
192 // reduce_apply - Reduce the result of applying a function to each value in a
193 // sequence, given an initial value, an operator, a function, and a sequence.
194 //
195 template
196 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func,
197 ValueType Value, TransFunc XForm) {
198 return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
199 Func, Value);
200 }
201 #endif
202
203
204 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
205 // to each value in a sequence. All of the bools returned by the mapped
206 // function are bitwise or'd together, and the result is returned.
207 //
208 template
209 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
210 return reduce_apply(First, Last, bitwise_or(), false, Func);
211 }
212
213
214 // map - This function maps the specified input sequence into the specified
215 // output iterator, applying a unary function in between.
216 //
217 template
218 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
219 return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
220 }
221 #endif
0 //===-- Support/StringExtras.h - Useful string functions ---------*- C++ -*--=//
1 //
2 // This file contains some functions that are useful when dealing with strings.
3 //
4 //===----------------------------------------------------------------------===//
5
6 #ifndef SUPPORT_STRING_EXTRAS_H
7 #define SUPPORT_STRING_EXTRAS_H
8
9 #include "Support/DataTypes.h"
10 #include
11 #include
12
13 static inline string utostr(uint64_t X, bool isNeg = false) {
14 char Buffer[40];
15 char *BufPtr = Buffer+39;
16
17 *BufPtr = 0; // Null terminate buffer...
18 if (X == 0) *--BufPtr = '0'; // Handle special case...
19
20 while (X) {
21 *--BufPtr = '0' + (X % 10);
22 X /= 10;
23 }
24
25 if (isNeg) *--BufPtr = '-'; // Add negative sign...
26
27 return string(BufPtr);
28 }
29
30 static inline string itostr(int64_t X) {
31 if (X < 0)
32 return utostr((uint64_t)-X, true);
33 else
34 return utostr((uint64_t)X);
35 }
36
37
38 static inline string utostr(unsigned X, bool isNeg = false) {
39 char Buffer[20];
40 char *BufPtr = Buffer+19;
41
42 *BufPtr = 0; // Null terminate buffer...
43 if (X == 0) *--BufPtr = '0'; // Handle special case...
44
45 while (X) {
46 *--BufPtr = '0' + (X % 10);
47 X /= 10;
48 }
49
50 if (isNeg) *--BufPtr = '-'; // Add negative sign...
51
52 return string(BufPtr);
53 }
54
55 static inline string itostr(int X) {
56 if (X < 0)
57 return utostr((unsigned)-X, true);
58 else
59 return utostr((unsigned)X);
60 }
61
62 static inline string ftostr(double V) {
63 char Buffer[200];
64 snprintf(Buffer, 200, "%e", V);
65 return Buffer;
66 }
67
68 #endif
0 //===- Support/Tree.h - Generic n-way tree structure -------------*- C++ -*--=//
1 //
2 // This class defines a generic N way tree node structure. The tree structure
3 // is immutable after creation, but the payload contained within it is not.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_SUPPORT_TREE_H
8 #define LLVM_SUPPORT_TREE_H
9
10 #include
11
12 template
13 class Tree {
14 vector Children; // This nodes children, if any
15 ConcreteTreeNode *Parent; // Parent of this node...
16 Payload Data; // Data held in this node...
17
18 protected:
19 void setChildren(const vector &children) {
20 Children = children;
21 }
22 public:
23 inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
24 inline Tree(const vector &children, ConcreteTreeNode *par)
25 : Children(children), Parent(par) {}
26
27 inline Tree(const vector &children, ConcreteTreeNode *par,
28 const Payload &data)
29 : Children(children), Parent(parent), Data(data) {}
30
31 // Tree dtor - Free all children
32 inline ~Tree() {
33 for (unsigned i = Children.size(); i > 0; --i)
34 delete Children[i-1];
35 }
36
37 // Tree manipulation/walking routines...
38 inline ConcreteTreeNode *getParent() const { return Parent; }
39 inline unsigned getNumChildren() const { return Children.size(); }
40 inline ConcreteTreeNode *getChild(unsigned i) const {
41 assert(i < Children.size() && "Tree::getChild with index out of range!");
42 return Children[i];
43 }
44
45 // Payload access...
46 inline Payload &getTreeData() { return Data; }
47 inline const Payload &getTreeData() const { return Data; }
48 };
49
50
51 #endif
0 //===- Support/DepthFirstIterator.h - Depth First iterator -------*- C++ -*--=//
1 //
2 // This file builds on the Support/GraphTraits.h file to build generic depth
3 // first graph iterator.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
8 #define LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
9
10 #include "Support/GraphTraits.h"
11 #include
12 #include
13 #include
14
15 // Generic Depth First Iterator
16 template >
17 class df_iterator : public std::forward_iterator
18 ptrdiff_t> {
19 typedef typename GT::NodeType NodeType;
20 typedef typename GT::ChildIteratorType ChildItTy;
21
22 set Visited; // All of the blocks visited so far...
23 // VisitStack - Used to maintain the ordering. Top = current block
24 // First element is node pointer, second is the 'next child' to visit
25 stack > VisitStack;
26 const bool Reverse; // Iterate over children before self?
27 private:
28 void reverseEnterNode() {
29 pair &Top = VisitStack.top();
30 NodeType *Node = Top.first;
31 ChildItTy &It = Top.second;
32 for (; It != GT::child_end(Node); ++It) {
33 NodeType *Child = *It;
34 if (!Visited.count(Child)) {
35 Visited.insert(Child);
36 VisitStack.push(make_pair(Child, GT::child_begin(Child)));
37 reverseEnterNode();
38 return;
39 }
40 }
41 }
42
43 inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) {
44 Visited.insert(Node);
45 VisitStack.push(make_pair(Node, GT::child_begin(Node)));
46 if (Reverse) reverseEnterNode();
47 }
48 inline df_iterator() { /* End is when stack is empty */ }
49
50 public:
51 typedef df_iterator _Self;
52
53 // Provide static begin and end methods as our public "constructors"
54 static inline _Self begin(GraphT G, bool Reverse = false) {
55 return _Self(GT::getEntryNode(G), Reverse);
56 }
57 static inline _Self end(GraphT G) { return _Self(); }
58
59
60 inline bool operator==(const _Self& x) const {
61 return VisitStack == x.VisitStack;
62 }
63 inline bool operator!=(const _Self& x) const { return !operator==(x); }
64
65 inline pointer operator*() const {
66 return VisitStack.top().first;
67 }
68
69 // This is a nonstandard operator-> that dereferences the pointer an extra
70 // time... so that you can actually call methods ON the Node, because
71 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
72 //
73 inline NodeType *operator->() const { return operator*(); }
74
75 inline _Self& operator++() { // Preincrement
76 if (Reverse) { // Reverse Depth First Iterator
77 if (VisitStack.top().second == GT::child_end(VisitStack.top().first))
78 VisitStack.pop();
79 if (!VisitStack.empty())
80 reverseEnterNode();
81 } else { // Normal Depth First Iterator
82 do {
83 pair &Top = VisitStack.top();
84 NodeType *Node = Top.first;
85 ChildItTy &It = Top.second;
86
87 while (It != GT::child_end(Node)) {
88 NodeType *Next = *It++;
89 if (!Visited.count(Next)) { // Has our next sibling been visited?
90 // No, do it now.
91 Visited.insert(Next);
92 VisitStack.push(make_pair(Next, GT::child_begin(Next)));
93 return *this;
94 }
95 }
96
97 // Oops, ran out of successors... go up a level on the stack.
98 VisitStack.pop();
99 } while (!VisitStack.empty());
100 }
101 return *this;
102 }
103
104 inline _Self operator++(int) { // Postincrement
105 _Self tmp = *this; ++*this; return tmp;
106 }
107
108 // nodeVisited - return true if this iterator has already visited the
109 // specified node. This is public, and will probably be used to iterate over
110 // nodes that a depth first iteration did not find: ie unreachable nodes.
111 //
112 inline bool nodeVisited(NodeType *Node) const {
113 return Visited.count(Node) != 0;
114 }
115 };
116
117
118 // Provide global constructors that automatically figure out correct types...
119 //
120 template
121 df_iterator df_begin(T G, bool Reverse = false) {
122 return df_iterator::begin(G, Reverse);
123 }
124
125 template
126 df_iterator df_end(T G) {
127 return df_iterator::end(G);
128 }
129
130 // Provide global definitions of inverse depth first iterators...
131 template
132 struct idf_iterator : public df_iterator > {
133 idf_iterator(const df_iterator > &V) :df_iterator >(V){}
134 };
135
136 template
137 idf_iterator idf_begin(T G, bool Reverse = false) {
138 return idf_iterator::begin(G, Reverse);
139 }
140
141 template
142 idf_iterator idf_end(T G){
143 return idf_iterator::end(G);
144 }
145
146 #endif
0 //===-- Support/GraphTraits.h - Graph traits template ------------*- C++ -*--=//
1 //
2 // This file defines the little GraphTraits template class that should be
3 // specialized by classes that want to be iteratable by generic graph iterators.
4 //
5 // This file also defines the marker class Inverse that is used to iterate over
6 // graphs in a graph defined, inverse ordering...
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_GRAPH_TRAITS_H
11 #define LLVM_SUPPORT_GRAPH_TRAITS_H
12
13 // GraphTraits - This class should be specialized by different graph types...
14 // which is why the default version is empty.
15 //
16 template
17 struct GraphTraits {
18 // Elements to provide:
19
20 // typedef NodeType - Type of Node in the graph
21 // typedef ChildIteratorType - Type used to iterate over children in graph
22
23 // static NodeType *getEntryNode(GraphType *)
24 // Return the entry node of the graph
25
26 // static ChildIteratorType child_begin(NodeType *)
27 // static ChildIteratorType child_end (NodeType *)
28 // Return iterators that point to the beginning and ending of the child
29 // node list for the specified node.
30 //
31
32
33 // If anyone tries to use this class without having an appropriate
34 // specialization, make an error. If you get this error, it's because you
35 // need to include the appropriate specialization of GraphTraits<> for your
36 // graph, or you need to define it for a new graph type. Either that or
37 // your argument to XXX_begin(...) is unknown or needs to have the proper .h
38 // file #include'd.
39 //
40 typedef typename GraphType::UnknownGraphTypeError NodeType;
41 };
42
43
44 // Inverse - This class is used as a little marker class to tell the graph
45 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
46 // Not all graphs define an inverse ordering, and if they do, it depends on
47 // the graph exactly what that is. Here's an example of usage with the
48 // df_iterator:
49 //
50 // idf_iterator I = idf_begin(M), E = idf_end(M);
51 // for (; I != E; ++I) { ... }
52 //
53 // Which is equivalent to:
54 // df_iterator > I = idf_begin(M), E = idf_end(M);
55 // for (; I != E; ++I) { ... }
56 //
57 template
58 struct Inverse {
59 GraphType &Graph;
60
61 inline Inverse(GraphType &G) : Graph(G) {}
62 };
63
64 #endif
0 //===-- HashExtras.h - Useful functions for STL hash containers --*- C++ -*--=//
1 //
2 // This file contains some templates that are useful if you are working with the
3 // STL Hashed containers.
4 //
5 // No library is required when using these functinons.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_HASHEXTRAS_H
10 #define LLVM_SUPPORT_HASHEXTRAS_H
11
12 #include
13 #include
14
15 template <> struct hash {
16 size_t operator()(string const &str) const {
17 return hash()(str.c_str());
18 }
19 };
20
21 // Provide a hash function for arbitrary pointers...
22 template struct hash {
23 inline size_t operator()(const T *Val) const { return (size_t)Val; }
24 };
25
26 #endif
0 //===-- Support/PostOrderIterator.h - Generic PostOrder iterator -*- C++ -*--=//
1 //
2 // This file builds on the Support/GraphTraits.h file to build a generic graph
3 // post order iterator. This should work over any graph type that has a
4 // GraphTraits specialization.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_SUPPORT_POSTORDER_ITERATOR_H
9 #define LLVM_SUPPORT_POSTORDER_ITERATOR_H
10
11 #include "Support/GraphTraits.h"
12 #include
13 #include
14 #include
15
16 template >
17 class po_iterator : public std::forward_iterator
18 ptrdiff_t> {
19 typedef typename GT::NodeType NodeType;
20 typedef typename GT::ChildIteratorType ChildItTy;
21
22 set Visited; // All of the blocks visited so far...
23 // VisitStack - Used to maintain the ordering. Top = current block
24 // First element is basic block pointer, second is the 'next child' to visit
25 stack > VisitStack;
26
27 void traverseChild() {
28 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
29 NodeType *BB = *VisitStack.top().second++;
30 if (!Visited.count(BB)) { // If the block is not visited...
31 Visited.insert(BB);
32 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
33 }
34 }
35 }
36
37 inline po_iterator(NodeType *BB) {
38 Visited.insert(BB);
39 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
40 traverseChild();
41 }
42 inline po_iterator() { /* End is when stack is empty */ }
43 public:
44 typedef po_iterator _Self;
45
46 // Provide static "constructors"...
47 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
48 static inline _Self end (GraphT G) { return _Self(); }
49
50 inline bool operator==(const _Self& x) const {
51 return VisitStack == x.VisitStack;
52 }
53 inline bool operator!=(const _Self& x) const { return !operator==(x); }
54
55 inline pointer operator*() const {
56 return VisitStack.top().first;
57 }
58
59 // This is a nonstandard operator-> that dereferences the pointer an extra
60 // time... so that you can actually call methods ON the BasicBlock, because
61 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
62 //
63 inline NodeType *operator->() const { return operator*(); }
64
65 inline _Self& operator++() { // Preincrement
66 VisitStack.pop();
67 if (!VisitStack.empty())
68 traverseChild();
69 return *this;
70 }
71
72 inline _Self operator++(int) { // Postincrement
73 _Self tmp = *this; ++*this; return tmp;
74 }
75 };
76
77 // Provide global constructors that automatically figure out correct types...
78 //
79 template
80 po_iterator po_begin(T G) { return po_iterator::begin(G); }
81 template
82 po_iterator po_end (T G) { return po_iterator::end(G); }
83
84 // Provide global definitions of inverse post order iterators...
85 template
86 struct ipo_iterator : public po_iterator > {
87 ipo_iterator(const po_iterator > &V) :po_iterator >(V){}
88 };
89
90 template
91 ipo_iterator ipo_begin(T G, bool Reverse = false) {
92 return ipo_iterator::begin(G, Reverse);
93 }
94
95 template
96 ipo_iterator ipo_end(T G){
97 return ipo_iterator::end(G);
98 }
99
100
101 //===--------------------------------------------------------------------===//
102 // Reverse Post Order CFG iterator code
103 //===--------------------------------------------------------------------===//
104 //
105 // This is used to visit basic blocks in a method in reverse post order. This
106 // class is awkward to use because I don't know a good incremental algorithm to
107 // computer RPO from a graph. Because of this, the construction of the
108 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
109 // with a postorder iterator to build the data structures). The moral of this
110 // story is: Don't create more ReversePostOrderTraversal classes than neccesary.
111 //
112 // This class should be used like this:
113 // {
114 // cfg::ReversePostOrderTraversal RPOT(MethodPtr); // Expensive to create
115 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
116 // ...
117 // }
118 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
119 // ...
120 // }
121 // }
122 //
123
124 typedef reverse_iterator::iterator> rpo_iterator;
125 // TODO: FIXME: ReversePostOrderTraversal is not generic!
126 class ReversePostOrderTraversal {
127 vector Blocks; // Block list in normal PO order
128 inline void Initialize(BasicBlock *BB) {
129 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
130 }
131 public:
132 inline ReversePostOrderTraversal(Method *M) {
133 Initialize(M->front());
134 }
135 inline ReversePostOrderTraversal(BasicBlock *BB) {
136 Initialize(BB);
137 }
138
139 // Because we want a reverse post order, use reverse iterators from the vector
140 inline rpo_iterator begin() { return Blocks.rbegin(); }
141 inline rpo_iterator end() { return Blocks.rend(); }
142 };
143
144 #endif
0 //===-- STLExtras.h - Useful functions when working with the STL -*- C++ -*--=//
1 //
2 // This file contains some templates that are useful if you are working with the
3 // STL at all.
4 //
5 // No library is required when using these functinons.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_STL_EXTRAS_H
10 #define LLVM_SUPPORT_STL_EXTRAS_H
11
12 #include
13
14 //===----------------------------------------------------------------------===//
15 // Extra additions to
16 //===----------------------------------------------------------------------===//
17
18 // bind_obj - Often times you want to apply the member function of an object
19 // as a unary functor. This macro is shorthand that makes it happen less
20 // verbosely.
21 //
22 // Example:
23 // struct Summer { void accumulate(int x); }
24 // vector Numbers;
25 // Summer MyS;
26 // for_each(Numbers.begin(), Numbers.end(),
27 // bind_obj(&MyS, &Summer::accumulate));
28 //
29 // TODO: When I get lots of extra time, convert this from an evil macro
30 //
31 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
32
33
34 // bitwise_or - This is a simple functor that applys operator| on its two
35 // arguments to get a boolean result.
36 //
37 template
38 struct bitwise_or : public binary_function {
39 bool operator()(const Ty& left, const Ty& right) const {
40 return left | right;
41 }
42 };
43
44
45 // deleter - Very very very simple method that is used to invoke operator
46 // delete on something. It is used like this:
47 //
48 // for_each(V.begin(), B.end(), deleter);
49 //
50 template
51 static inline void deleter(T *Ptr) {
52 delete Ptr;
53 }
54
55
56
57 //===----------------------------------------------------------------------===//
58 // Extra additions to
59 //===----------------------------------------------------------------------===//
60
61 // mapped_iterator - This is a simple iterator adapter that causes a function to
62 // be dereferenced whenever operator* is invoked on the iterator.
63 //
64 // It turns out that this is disturbingly similar to boost::transform_iterator
65 //
66 #if 1
67 template
68 class mapped_iterator {
69 RootIt current;
70 UnaryFunc Fn;
71 public:
72 typedef typename iterator_traits::iterator_category
73 iterator_category;
74 typedef typename iterator_traits::difference_type
75 difference_type;
76 typedef typename UnaryFunc::result_type value_type;
77 typedef typename UnaryFunc::result_type *pointer;
78 typedef void reference; // Can't modify value returned by fn
79
80 typedef RootIt iterator_type;
81 typedef mapped_iterator _Self;
82
83 inline RootIt &getCurrent() const { return current; }
84
85 inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
86 : current(I), Fn(F) {}
87 inline mapped_iterator(const mapped_iterator &It)
88 : current(It.current), Fn(It.Fn) {}
89
90 inline value_type operator*() const { // All this work to do this
91 return Fn(*current); // little change
92 }
93
94 _Self& operator++() { ++current; return *this; }
95 _Self& operator--() { --current; return *this; }
96 _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
97 _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; }
98 _Self operator+ (difference_type n) const { return _Self(current + n); }
99 _Self& operator+= (difference_type n) { current += n; return *this; }
100 _Self operator- (difference_type n) const { return _Self(current - n); }
101 _Self& operator-= (difference_type n) { current -= n; return *this; }
102 reference operator[](difference_type n) const { return *(*this + n); }
103
104 inline bool operator==(const _Self &X) const { return current == X.current; }
105 inline bool operator< (const _Self &X) const { return current < X.current; }
106
107 inline difference_type operator-(const _Self &X) const {
108 return current - X.current;
109 }
110 };
111
112 template
113 inline mapped_iterator<_Iterator, Func>
114 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
115 const mapped_iterator<_Iterator, Func>& X) {
116 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
117 }
118
119 #else
120
121 // This fails to work, because some iterators are not classes, for example
122 // vector iterators are commonly value_type **'s
123 template
124 class mapped_iterator : public RootIt {
125 UnaryFunc Fn;
126 public:
127 typedef typename UnaryFunc::result_type value_type;
128 typedef typename UnaryFunc::result_type *pointer;
129 typedef void reference; // Can't modify value returned by fn
130
131 typedef mapped_iterator _Self;
132 typedef RootIt super;
133 inline explicit mapped_iterator(const RootIt &I) : super(I) {}
134 inline mapped_iterator(const super &It) : super(It) {}
135
136 inline value_type operator*() const { // All this work to do
137 return Fn(super::operator*()); // this little thing
138 }
139 };
140 #endif
141
142 // map_iterator - Provide a convenient way to create mapped_iterators, just like
143 // make_pair is useful for creating pairs...
144 //
145 template
146 inline mapped_iterator map_iterator(const ItTy &I, FuncTy F) {
147 return mapped_iterator(I, F);
148 }
149
150
151 //===----------------------------------------------------------------------===//
152 // Extra additions to
153 //===----------------------------------------------------------------------===//
154
155 // apply_until - Apply a functor to a sequence continually, unless the
156 // functor returns true. Return true if the functor returned true, return false
157 // if the functor never returned true.
158 //
159 template
160 bool apply_until(InputIt First, InputIt Last, Function Func) {
161 for ( ; First != Last; ++First)
162 if (Func(*First)) return true;
163 return false;
164 }
165
166
167 // reduce - Reduce a sequence values into a single value, given an initial
168 // value and an operator.
169 //
170 template
171 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
172 for ( ; First != Last; ++First)
173 Value = Func(*First, Value);
174 return Value;
175 }
176
177 #if 1 // This is likely to be more efficient
178
179 // reduce_apply - Reduce the result of applying a function to each value in a
180 // sequence, given an initial value, an operator, a function, and a sequence.
181 //
182 template
183 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func,
184 ValueType Value, TransFunc XForm) {
185 for ( ; First != Last; ++First)
186 Value = Func(XForm(*First), Value);
187 return Value;
188 }
189
190 #else // This is arguably more elegant
191
192 // reduce_apply - Reduce the result of applying a function to each value in a
193 // sequence, given an initial value, an operator, a function, and a sequence.
194 //
195 template
196 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func,
197 ValueType Value, TransFunc XForm) {
198 return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
199 Func, Value);
200 }
201 #endif
202
203
204 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
205 // to each value in a sequence. All of the bools returned by the mapped
206 // function are bitwise or'd together, and the result is returned.
207 //
208 template
209 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
210 return reduce_apply(First, Last, bitwise_or(), false, Func);
211 }
212
213
214 // map - This function maps the specified input sequence into the specified
215 // output iterator, applying a unary function in between.
216 //
217 template
218 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
219 return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
220 }
221 #endif
0 //===-- Support/StringExtras.h - Useful string functions ---------*- C++ -*--=//
1 //
2 // This file contains some functions that are useful when dealing with strings.
3 //
4 //===----------------------------------------------------------------------===//
5
6 #ifndef SUPPORT_STRING_EXTRAS_H
7 #define SUPPORT_STRING_EXTRAS_H
8
9 #include "Support/DataTypes.h"
10 #include
11 #include
12
13 static inline string utostr(uint64_t X, bool isNeg = false) {
14 char Buffer[40];
15 char *BufPtr = Buffer+39;
16
17 *BufPtr = 0; // Null terminate buffer...
18 if (X == 0) *--BufPtr = '0'; // Handle special case...
19
20 while (X) {
21 *--BufPtr = '0' + (X % 10);
22 X /= 10;
23 }
24
25 if (isNeg) *--BufPtr = '-'; // Add negative sign...
26
27 return string(BufPtr);
28 }
29
30 static inline string itostr(int64_t X) {
31 if (X < 0)
32 return utostr((uint64_t)-X, true);
33 else
34 return utostr((uint64_t)X);
35 }
36
37
38 static inline string utostr(unsigned X, bool isNeg = false) {
39 char Buffer[20];
40 char *BufPtr = Buffer+19;
41
42 *BufPtr = 0; // Null terminate buffer...
43 if (X == 0) *--BufPtr = '0'; // Handle special case...
44
45 while (X) {
46 *--BufPtr = '0' + (X % 10);
47 X /= 10;
48 }
49
50 if (isNeg) *--BufPtr = '-'; // Add negative sign...
51
52 return string(BufPtr);
53 }
54
55 static inline string itostr(int X) {
56 if (X < 0)
57 return utostr((unsigned)-X, true);
58 else
59 return utostr((unsigned)X);
60 }
61
62 static inline string ftostr(double V) {
63 char Buffer[200];
64 snprintf(Buffer, 200, "%e", V);
65 return Buffer;
66 }
67
68 #endif
0 //===- Support/Tree.h - Generic n-way tree structure -------------*- C++ -*--=//
1 //
2 // This class defines a generic N way tree node structure. The tree structure
3 // is immutable after creation, but the payload contained within it is not.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_SUPPORT_TREE_H
8 #define LLVM_SUPPORT_TREE_H
9
10 #include
11
12 template
13 class Tree {
14 vector Children; // This nodes children, if any
15 ConcreteTreeNode *Parent; // Parent of this node...
16 Payload Data; // Data held in this node...
17
18 protected:
19 void setChildren(const vector &children) {
20 Children = children;
21 }
22 public:
23 inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
24 inline Tree(const vector &children, ConcreteTreeNode *par)
25 : Children(children), Parent(par) {}
26
27 inline Tree(const vector &children, ConcreteTreeNode *par,
28 const Payload &data)
29 : Children(children), Parent(parent), Data(data) {}
30
31 // Tree dtor - Free all children
32 inline ~Tree() {
33 for (unsigned i = Children.size(); i > 0; --i)
34 delete Children[i-1];
35 }
36
37 // Tree manipulation/walking routines...
38 inline ConcreteTreeNode *getParent() const { return Parent; }
39 inline unsigned getNumChildren() const { return Children.size(); }
40 inline ConcreteTreeNode *getChild(unsigned i) const {
41 assert(i < Children.size() && "Tree::getChild with index out of range!");
42 return Children[i];
43 }
44
45 // Payload access...
46 inline Payload &getTreeData() { return Data; }
47 inline const Payload &getTreeData() const { return Data; }
48 };
49
50
51 #endif
1515 #ifndef LLVM_ANALYSIS_CALLGRAPH_H
1616 #define LLVM_ANALYSIS_CALLGRAPH_H
1717
18 #include "llvm/Support/GraphTraits.h"
18 #include "Support/GraphTraits.h"
1919 #include
2020 #include
2121 class Method;
1313 #ifndef LLVM_ANALYSIS_INSTFOREST_H
1414 #define LLVM_ANALYSIS_INSTFOREST_H
1515
16 #include "llvm/Support/Tree.h"
1716 #include "llvm/Instruction.h"
17 #include "Support/Tree.h"
1818 #include
1919
2020 namespace analysis {
2323
2424 #include "llvm/Value.h"
2525 #include "llvm/ValueHolder.h"
26 #include "llvm/Support/GraphTraits.h"
2726 #include "llvm/InstrTypes.h"
27 #include "Support/GraphTraits.h"
2828 #include
2929
3030 class Instruction;
2323 #ifndef LLVM_CODEGEN_INSTRFOREST_H
2424 #define LLVM_CODEGEN_INSTRFOREST_H
2525
26 #include "llvm/Support/NonCopyable.h"
27 #include "llvm/Support/HashExtras.h"
2826 #include "llvm/Instruction.h"
27 #include "Support/NonCopyable.h"
28 #include "Support/HashExtras.h"
2929 #include
3030 #include
3131
1111 #ifndef LLVM_CODEGEN_INSTR_SCHEDULING_H
1212 #define LLVM_CODEGEN_INSTR_SCHEDULING_H
1313
14 #include "llvm/Support/CommandLine.h"
1514 #include "llvm/CodeGen/MachineInstr.h"
15 #include "Support/CommandLine.h"
1616
1717 class Method;
1818 class SchedulingManager;
1414 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
1515 #define LLVM_CODEGEN_MACHINEINSTR_H
1616
17 #include
17 #include "Support/DataTypes.h"
18 #include "Support/NonCopyable.h"
1819 #include "llvm/CodeGen/InstrForest.h"
19 #include "Support/DataTypes.h"
20 #include "llvm/Support/NonCopyable.h"
2120 #include "llvm/Target/MachineInstrInfo.h"
2221 #include "llvm/Annotation.h"
2322 #include "llvm/Method.h"
23 #include
2424 #include
2525 #include
2626 #include
0
1 #include "llvm/Support/CommandLine.h"
1 #include "Support/CommandLine.h"
22
33 #ifndef REG_ALLOC_COMMON_H
44 #define REG_ALLOC_COMMON_H
0 //===- Support/CommandLine.h - Flexible Command line parser ------*- C++ -*--=//
1 //
2 // This class implements a command line argument processor that is useful when
3 // creating a tool. It provides a simple, minimalistic interface that is easily
4 // extensible and supports nonlocal (library) command line options.
5 //
6 // Note that rather than trying to figure out what this code does, you could try
7 // reading the library documentation located in docs/CommandLine.html
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_SUPPORT_COMMANDLINE_H
12 #define LLVM_SUPPORT_COMMANDLINE_H
13
14 #include
15 #include
16 #include
17 #include
18
19 namespace cl { // Short namespace to make usage concise
20
21 //===----------------------------------------------------------------------===//
22 // ParseCommandLineOptions - Minimalistic command line option processing entry
23 //
24 void cl::ParseCommandLineOptions(int &argc, char **argv,
25 const char *Overview = 0,
26 int Flags = 0);
27
28 // ParserOptions - This set of option is use to control global behavior of the
29 // command line processor.
30 //
31 enum ParserOptions {
32 // DisableSingleLetterArgGrouping - With this option enabled, multiple letter
33 // options are allowed to bunch together with only a single hyphen for the
34 // whole group. This allows emulation of the behavior that ls uses for
35 // example: ls -la === ls -l -a Providing this option, disables this.
36 //
37 DisableSingleLetterArgGrouping = 0x0001,
38
39 // EnableSingleLetterArgValue - This option allows arguments that are
40 // otherwise unrecognized to match single letter flags that take a value.
41 // This is useful for cases like a linker, where options are typically of the
42 // form '-lfoo' or '-L../../include' where -l or -L are the actual flags.
43 //
44 EnableSingleLetterArgValue = 0x0002,
45 };
46
47
48 //===----------------------------------------------------------------------===//
49 // Global flags permitted to be passed to command line arguments
50
51 enum FlagsOptions {
52 NoFlags = 0x00, // Marker to make explicit that we have no flags
53 Default = 0x00, // Equally, marker to use the default flags
54
55 GlobalsMask = 0x80,
56 };
57
58 enum NumOccurances { // Flags for the number of occurances allowed...
59 Optional = 0x01, // Zero or One occurance
60 ZeroOrMore = 0x02, // Zero or more occurances allowed
61 Required = 0x03, // One occurance required
62 OneOrMore = 0x04, // One or more occurances required
63
64 // ConsumeAfter - Marker for a null ("") flag that can be used to indicate
65 // that anything that matches the null marker starts a sequence of options
66 // that all get sent to the null marker. Thus, for example, all arguments
67 // to LLI are processed until a filename is found. Once a filename is found,
68 // all of the succeeding arguments are passed, unprocessed, to the null flag.
69 //
70 ConsumeAfter = 0x05,
71
72 OccurancesMask = 0x07,
73 };
74
75 enum ValueExpected { // Is a value required for the option?
76 ValueOptional = 0x08, // The value can oppear... or not
77 ValueRequired = 0x10, // The value is required to appear!
78 ValueDisallowed = 0x18, // A value may not be specified (for flags)
79 ValueMask = 0x18,
80 };
81
82 enum OptionHidden { // Control whether -help shows this option
83 NotHidden = 0x20, // Option included in --help & --help-hidden
84 Hidden = 0x40, // -help doesn't, but --help-hidden does
85 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
86 HiddenMask = 0x60,
87 };
88
89
90 //===----------------------------------------------------------------------===//
91 // Option Base class
92 //
93 class Alias;
94 class Option {
95 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
96 friend class Alias;
97
98 // handleOccurances - Overriden by subclasses to handle the value passed into
99 // an argument. Should return true if there was an error processing the
100 // argument and the program should exit.
101 //
102 virtual bool handleOccurance(const char *ArgName, const string &Arg) = 0;
103
104 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
105 return Optional;
106 }
107 virtual enum ValueExpected getValueExpectedFlagDefault() const {
108 return ValueOptional;
109 }
110 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
111 return NotHidden;
112 }
113
114 int NumOccurances; // The number of times specified
115 const int Flags; // Flags for the argument
116 public:
117 const char * const ArgStr; // The argument string itself (ex: "help", "o")
118 const char * const HelpStr; // The descriptive text message for --help
119
120 inline enum NumOccurances getNumOccurancesFlag() const {
121 int NO = Flags & OccurancesMask;
122 return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
123 }
124 inline enum ValueExpected getValueExpectedFlag() const {
125 int VE = Flags & ValueMask;
126 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
127 }
128 inline enum OptionHidden getOptionHiddenFlag() const {
129 int OH = Flags & HiddenMask;
130 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
131 }
132
133 protected:
134 Option(const char *ArgStr, const char *Message, int Flags);
135 Option(int flags) : NumOccurances(0), Flags(flags), ArgStr(""), HelpStr("") {}
136
137 public:
138 // Return the width of the option tag for printing...
139 virtual unsigned getOptionWidth() const;
140
141 // printOptionInfo - Print out information about this option. The
142 // to-be-maintained width is specified.
143 //
144 virtual void printOptionInfo(unsigned GlobalWidth) const;
145
146 // addOccurance - Wrapper around handleOccurance that enforces Flags
147 //
148 bool addOccurance(const char *ArgName, const string &Value);
149
150 // Prints option name followed by message. Always returns true.
151 bool error(string Message, const char *ArgName = 0);
152
153 public:
154 inline int getNumOccurances() const { return NumOccurances; }
155 virtual ~Option() {}
156 };
157
158
159 //===----------------------------------------------------------------------===//
160 // Aliased command line option (alias this name to a preexisting name)
161 //
162 class Alias : public Option {
163 Option &AliasFor;
164 virtual bool handleOccurance(const char *ArgName, const string &Arg) {
165 return AliasFor.handleOccurance(AliasFor.ArgStr, Arg);
166 }
167 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
168 public:
169 inline Alias(const char *ArgStr, const char *Message, int Flags,
170 Option &aliasFor) : Option(ArgStr, Message, Flags),
171 AliasFor(aliasFor) {}
172 };
173
174 //===----------------------------------------------------------------------===//
175 // Boolean/flag command line option
176 //
177 class Flag : public Option {
178 bool Value;
179 virtual bool handleOccurance(const char *ArgName, const string &Arg);
180 public:
181 inline Flag(const char *ArgStr, const char *Message, int Flags = 0,
182 bool DefaultVal = 0) : Option(ArgStr, Message, Flags),
183 Value(DefaultVal) {}
184 operator const bool() const { return Value; }
185 inline bool operator=(bool Val) { Value = Val; return Val; }
186 };
187
188
189
190 //===----------------------------------------------------------------------===//
191 // Integer valued command line option
192 //
193 class Int : public Option {
194 int Value;
195 virtual bool handleOccurance(const char *ArgName, const string &Arg);
196 virtual enum ValueExpected getValueExpectedFlagDefault() const {
197 return ValueRequired;
198 }
199 public:
200 inline Int(const char *ArgStr, const char *Help, int Flags = 0,
201 int DefaultVal = 0) : Option(ArgStr, Help, Flags),
202 Value(DefaultVal) {}
203 inline operator int() const { return Value; }
204 inline int operator=(int Val) { Value = Val; return Val; }
205 };
206
207
208 //===----------------------------------------------------------------------===//
209 // String valued command line option
210 //
211 class String : public Option, public string {
212 virtual bool handleOccurance(const char *ArgName, const string &Arg);
213 virtual enum ValueExpected getValueExpectedFlagDefault() const {
214 return ValueRequired;
215 }
216 public:
217 inline String(const char *ArgStr, const char *Help, int Flags = 0,
218 const char *DefaultVal = "")
219 : Option(ArgStr, Help, Flags), string(DefaultVal) {}
220
221 inline const string &operator=(const string &Val) {
222 return string::operator=(Val);
223 }
224 };
225
226
227 //===----------------------------------------------------------------------===//
228 // String list command line option
229 //
230 class StringList : public Option, public vector {
231
232 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
233 return ZeroOrMore;
234 }
235 virtual enum ValueExpected getValueExpectedFlagDefault() const {
236 return ValueRequired;
237 }
238 virtual bool handleOccurance(const char *ArgName, const string &Arg);
239
240 public:
241 inline StringList(const char *ArgStr, const char *Help, int Flags = 0)
242 : Option(ArgStr, Help, Flags) {}
243 };
244
245
246 //===----------------------------------------------------------------------===//
247 // Enum valued command line option
248 //
249 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, ENUMVAL, DESC
250 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, ENUMVAL, DESC
251
252 // EnumBase - Base class for all enum/varargs related argument types...
253 class EnumBase : public Option {
254 protected:
255 // Use a vector instead of a map, because the lists should be short,
256 // the overhead is less, and most importantly, it keeps them in the order
257 // inserted so we can print our option out nicely.
258 vector > > ValueMap;
259
260 inline EnumBase(const char *ArgStr, const char *Help, int Flags)
261 : Option(ArgStr, Help, Flags) {}
262 inline EnumBase(int Flags) : Option(Flags) {}
263
264 // processValues - Incorporate the specifed varargs arglist into the
265 // ValueMap.
266 //
267 void processValues(va_list Vals);
268
269 // registerArgs - notify the system about these new arguments
270 void registerArgs();
271
272 public:
273 // Turn an enum into the arg name that activates it
274 const char *getArgName(int ID) const;
275 const char *getArgDescription(int ID) const;
276 };
277
278 class EnumValueBase : public EnumBase {
279 protected:
280 int Value;
281 inline EnumValueBase(const char *ArgStr, const char *Help, int Flags)
282 : EnumBase(ArgStr, Help, Flags) {}
283 inline EnumValueBase(int Flags) : EnumBase(Flags) {}
284
285 // handleOccurance - Set Value to the enum value specified by Arg
286 virtual bool handleOccurance(const char *ArgName, const string &Arg);
287
288 // Return the width of the option tag for printing...
289 virtual unsigned getOptionWidth() const;
290
291 // printOptionInfo - Print out information about this option. The
292 // to-be-maintained width is specified.
293 //
294 virtual void printOptionInfo(unsigned GlobalWidth) const;
295 };
296
297 template // The enum we are representing
298 class Enum : public EnumValueBase {
299 virtual enum ValueExpected getValueExpectedFlagDefault() const {
300 return ValueRequired;
301 }
302 public:
303 inline Enum(const char *ArgStr, int Flags, const char *Help, ...)
304 : EnumValueBase(ArgStr, Help, Flags) {
305 va_list Values;
306 va_start(Values, Help);
307 processValues(Values);
308 va_end(Values);
309 Value = ValueMap.front().second.first; // Grab default value
310 }
311
312 inline operator E() const { return (E)Value; }
313 inline E operator=(E Val) { Value = Val; return Val; }
314 };
315
316
317 //===----------------------------------------------------------------------===//
318 // Enum flags command line option
319 //
320 class EnumFlagsBase : public EnumValueBase {
321 virtual enum ValueExpected getValueExpectedFlagDefault() const {
322 return ValueDisallowed;
323 }
324 protected:
325 virtual bool handleOccurance(const char *ArgName, const string &Arg);
326 inline EnumFlagsBase(int Flags) : EnumValueBase(Flags) {}
327
328 // Return the width of the option tag for printing...
329 virtual unsigned getOptionWidth() const;
330
331 // printOptionInfo - Print out information about this option. The
332 // to-be-maintained width is specified.
333 //
334 virtual void printOptionInfo(unsigned GlobalWidth) const;
335 };
336
337 template // The enum we are representing
338 class EnumFlags : public EnumFlagsBase {
339 public:
340 inline EnumFlags(int Flags, ...) : EnumFlagsBase(Flags) {
341 va_list Values;
342 va_start(Values, Flags);
343 processValues(Values);
344 va_end(Values);
345 registerArgs();
346 Value = ValueMap.front().second.first; // Grab default value
347 }
348
349 inline operator E() const { return (E)Value; }
350 inline E operator=(E Val) { Value = Val; return Val; }
351 };
352
353
354 //===----------------------------------------------------------------------===//
355 // Enum list command line option
356 //
357 class EnumListBase : public EnumBase {
358 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
359 return ZeroOrMore;
360 }
361 virtual enum ValueExpected getValueExpectedFlagDefault() const {
362 return ValueDisallowed;
363 }
364 protected:
365 vector Values; // The options specified so far.
366
367 inline EnumListBase(int Flags)
368 : EnumBase(Flags) {}
369 virtual bool handleOccurance(const char *ArgName, const string &Arg);
370
371 // Return the width of the option tag for printing...
372 virtual unsigned getOptionWidth() const;
373
374 // printOptionInfo - Print out information about this option. The
375 // to-be-maintained width is specified.
376 //
377 virtual void printOptionInfo(unsigned GlobalWidth) const;
378 public:
379 inline unsigned size() { return Values.size(); }
380 };
381
382 template // The enum we are representing
383 class EnumList : public EnumListBase {
384 public:
385 inline EnumList(int Flags, ...) : EnumListBase(Flags) {
386 va_list Values;
387 va_start(Values, Flags);
388 processValues(Values);
389 va_end(Values);
390 registerArgs();
391 }
392 inline E operator[](unsigned i) const { return (E)Values[i]; }
393 inline E &operator[](unsigned i) { return (E&)Values[i]; }
394 };
395
396 } // End namespace cl
397
398 #endif
+0
-147
include/llvm/Support/DepthFirstIterator.h less more
None //===- llvm/Support/DepthFirstIterator.h - Depth First iterators -*- C++ -*--=//
1 //
2 // This file builds on the Support/GraphTraits.h file to build generic depth
3 // first graph iterator.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
8 #define LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
9
10 #include "llvm/Support/GraphTraits.h"
11 #include
12 #include
13 #include
14
15 // Generic Depth First Iterator
16 template >
17 class df_iterator : public std::forward_iterator
18 ptrdiff_t> {
19 typedef typename GT::NodeType NodeType;
20 typedef typename GT::ChildIteratorType ChildItTy;
21
22 set Visited; // All of the blocks visited so far...
23 // VisitStack - Used to maintain the ordering. Top = current block
24 // First element is node pointer, second is the 'next child' to visit
25 stack > VisitStack;
26 const bool Reverse; // Iterate over children before self?
27 private:
28 void reverseEnterNode() {
29 pair &Top = VisitStack.top();
30 NodeType *Node = Top.first;
31 ChildItTy &It = Top.second;
32 for (; It != GT::child_end(Node); ++It) {
33 NodeType *Child = *It;
34 if (!Visited.count(Child)) {
35 Visited.insert(Child);
36 VisitStack.push(make_pair(Child, GT::child_begin(Child)));
37 reverseEnterNode();
38 return;
39 }
40 }
41 }
42
43 inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) {
44 Visited.insert(Node);
45 VisitStack.push(make_pair(Node, GT::child_begin(Node)));
46 if (Reverse) reverseEnterNode();
47 }
48 inline df_iterator() { /* End is when stack is empty */ }
49
50 public:
51 typedef df_iterator _Self;
52
53 // Provide static begin and end methods as our public "constructors"
54 static inline _Self begin(GraphT G, bool Reverse = false) {
55 return _Self(GT::getEntryNode(G), Reverse);
56 }
57 static inline _Self end(GraphT G) { return _Self(); }
58
59
60 inline bool operator==(const _Self& x) const {
61 return VisitStack == x.VisitStack;
62 }
63 inline bool operator!=(const _Self& x) const { return !operator==(x); }
64
65 inline pointer operator*() const {
66 return VisitStack.top().first;
67 }
68
69 // This is a nonstandard operator-> that dereferences the pointer an extra
70 // time... so that you can actually call methods ON the Node, because
71 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
72 //
73 inline NodeType *operator->() const { return operator*(); }
74
75 inline _Self& operator++() { // Preincrement
76 if (Reverse) { // Reverse Depth First Iterator
77 if (VisitStack.top().second == GT::child_end(VisitStack.top().first))
78 VisitStack.pop();
79 if (!VisitStack.empty())
80 reverseEnterNode();
81 } else { // Normal Depth First Iterator
82 do {
83 pair &Top = VisitStack.top();
84 NodeType *Node = Top.first;
85 ChildItTy &It = Top.second;
86
87 while (It != GT::child_end(Node)) {
88 NodeType *Next = *It++;
89 if (!Visited.count(Next)) { // Has our next sibling been visited?
90 // No, do it now.
91 Visited.insert(Next);
92 VisitStack.push(make_pair(Next, GT::child_begin(Next)));
93 return *this;
94 }
95 }
96
97 // Oops, ran out of successors... go up a level on the stack.
98 VisitStack.pop();
99 } while (!VisitStack.empty());
100 }
101 return *this;
102 }
103
104 inline _Self operator++(int) { // Postincrement
105 _Self tmp = *this; ++*this; return tmp;
106 }
107
108 // nodeVisited - return true if this iterator has already visited the
109 // specified node. This is public, and will probably be used to iterate over
110 // nodes that a depth first iteration did not find: ie unreachable nodes.
111 //
112 inline bool nodeVisited(NodeType *Node) const {
113 return Visited.count(Node) != 0;
114 }
115 };
116
117
118 // Provide global constructors that automatically figure out correct types...
119 //
120 template
121 df_iterator df_begin(T G, bool Reverse = false) {
122 return df_iterator::begin(G, Reverse);
123 }
124
125 template
126 df_iterator df_end(T G) {
127 return df_iterator::end(G);
128 }
129
130 // Provide global definitions of inverse depth first iterators...
131 template
132 struct idf_iterator : public df_iterator > {
133 idf_iterator(const df_iterator > &V) :df_iterator >(V){}
134 };
135
136 template
137 idf_iterator idf_begin(T G, bool Reverse = false) {
138 return idf_iterator::begin(G, Reverse);
139 }
140
141 template
142 idf_iterator idf_end(T G){
143 return idf_iterator::end(G);
144 }
145
146 #endif
+0
-65
include/llvm/Support/GraphTraits.h less more
None //===-- llvm/Support/GraphTraits.h - Graph traits template -------*- C++ -*--=//
1 //
2 // This file defines the little GraphTraits template class that should be
3 // specialized by classes that want to be iteratable by generic graph iterators.
4 //
5 // This file also defines the marker class Inverse that is used to iterate over
6 // graphs in a graph defined, inverse ordering...
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_GRAPH_TRAITS_H
11 #define LLVM_SUPPORT_GRAPH_TRAITS_H
12
13 // GraphTraits - This class should be specialized by different graph types...
14 // which is why the default version is empty.
15 //
16 template
17 struct GraphTraits {
18 // Elements to provide:
19
20 // typedef NodeType - Type of Node in the graph
21 // typedef ChildIteratorType - Type used to iterate over children in graph
22
23 // static NodeType *getEntryNode(GraphType *)
24 // Return the entry node of the graph
25
26 // static ChildIteratorType child_begin(NodeType *)
27 // static ChildIteratorType child_end (NodeType *)
28 // Return iterators that point to the beginning and ending of the child
29 // node list for the specified node.
30 //
31
32
33 // If anyone tries to use this class without having an appropriate
34 // specialization, make an error. If you get this error, it's because you
35 // need to include the appropriate specialization of GraphTraits<> for your
36 // graph, or you need to define it for a new graph type. Either that or
37 // your argument to XXX_begin(...) is unknown or needs to have the proper .h
38 // file #include'd.
39 //
40 typedef typename GraphType::UnknownGraphTypeError NodeType;
41 };
42
43
44 // Inverse - This class is used as a little marker class to tell the graph
45 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
46 // Not all graphs define an inverse ordering, and if they do, it depends on
47 // the graph exactly what that is. Here's an example of usage with the
48 // df_iterator:
49 //
50 // idf_iterator I = idf_begin(M), E = idf_end(M);
51 // for (; I != E; ++I) { ... }
52 //
53 // Which is equivalent to:
54 // df_iterator > I = idf_begin(M), E = idf_end(M);
55 // for (; I != E; ++I) { ... }
56 //
57 template
58 struct Inverse {
59 GraphType &Graph;
60
61 inline Inverse(GraphType &G) : Graph(G) {}
62 };
63
64 #endif
+0
-27
include/llvm/Support/HashExtras.h less more
None //===-- HashExtras.h - Useful functions for STL hash containers --*- C++ -*--=//
1 //
2 // This file contains some templates that are useful if you are working with the
3 // STL Hashed containers.
4 //
5 // No library is required when using these functinons.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_HASHEXTRAS_H
10 #define LLVM_SUPPORT_HASHEXTRAS_H
11
12 #include
13 #include
14
15 template <> struct hash {
16 size_t operator()(string const &str) const {
17 return hash()(str.c_str());
18 }
19 };
20
21 // Provide a hash function for arbitrary pointers...
22 template struct hash {
23 inline size_t operator()(const T *Val) const { return (size_t)Val; }
24 };
25
26 #endif
0 // $Id$ -*-c++-*-
1 //***************************************************************************
2 // File:
3 // MathExtras.h
4 //
5 // Purpose:
6 //
7 // History:
8 // 8/25/01 - Vikram Adve - Created
9 //**************************************************************************/
10
11 #ifndef LLVM_SUPPORT_MATH_EXTRAS_H
12 #define LLVM_SUPPORT_MATH_EXTRAS_H
13
14 #include
15
16 inline bool IsPowerOf2 (int64_t C, unsigned& getPow);
17
18 inline
19 bool IsPowerOf2(int64_t C, unsigned& getPow)
20 {
21 if (C < 0)
22 C = -C;
23 bool isBool = C > 0 && (C == (C & ~(C - 1)));
24 if (isBool)
25 for (getPow = 0; C > 1; getPow++)
26 C = C >> 1;
27
28 return isBool;
29 }
30
31 #endif /*LLVM_SUPPORT_MATH_EXTRAS_H*/
+0
-37
include/llvm/Support/NonCopyable.h less more
None //===-- NonCopyable.h - Disable copy ctor and op= in subclasses --*- C++ -*--=//
1 //
2 // This file defines the NonCopyable and NonCopyableV classes. These mixin
3 // classes may be used to mark a class not being copyable. You should derive
4 // from NonCopyable if you don't want to have a virtual dtor, or NonCopyableV
5 // if you do want polymorphic behavior in your class.
6 //
7 // No library is required when using these functinons.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_SUPPORT_NONCOPYABLE_H
12 #define LLVM_SUPPORT_NONCOPYABLE_H
13
14 class NonCopyable {
15 // Disable the copy constructor and the assignment operator
16 // by making them both private:
17 //
18 NonCopyable(const NonCopyable &); // DO NOT IMPLEMENT
19 NonCopyable &operator=(const NonCopyable &); // DO NOT IMPLEMENT
20 protected:
21 inline NonCopyable() {}
22 inline ~NonCopyable() {}
23 };
24
25 class NonCopyableV {
26 // Disable the copy constructor and the assignment operator
27 // by making them both private:
28 //
29 NonCopyableV(const NonCopyableV &); // DO NOT IMPLEMENT
30 NonCopyableV &operator=(const NonCopyableV &); // DO NOT IMPLEMENT
31 protected:
32 inline NonCopyableV() {}
33 virtual ~NonCopyableV() {}
34 };
35
36 #endif
+0
-145
include/llvm/Support/PostOrderIterator.h less more
None //===-- llvm/Support/PostOrderIterator.h - Generic PO iterator ---*- C++ -*--=//
1 //
2 // This file builds on the Support/GraphTraits.h file to build a generic graph
3 // post order iterator. This should work over any graph type that has a
4 // GraphTraits specialization.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_SUPPORT_POSTORDER_ITERATOR_H
9 #define LLVM_SUPPORT_POSTORDER_ITERATOR_H
10
11 #include "llvm/Support/GraphTraits.h"
12 #include
13 #include
14 #include
15
16 template >
17 class po_iterator : public std::forward_iterator
18 ptrdiff_t> {
19 typedef typename GT::NodeType NodeType;
20 typedef typename GT::ChildIteratorType ChildItTy;
21
22 set Visited; // All of the blocks visited so far...
23 // VisitStack - Used to maintain the ordering. Top = current block
24 // First element is basic block pointer, second is the 'next child' to visit
25 stack > VisitStack;
26
27 void traverseChild() {
28 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
29 NodeType *BB = *VisitStack.top().second++;
30 if (!Visited.count(BB)) { // If the block is not visited...
31 Visited.insert(BB);
32 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
33 }
34 }
35 }
36
37 inline po_iterator(NodeType *BB) {
38 Visited.insert(BB);
39 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
40 traverseChild();
41 }
42 inline po_iterator() { /* End is when stack is empty */ }
43 public:
44 typedef po_iterator _Self;
45
46 // Provide static "constructors"...
47 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
48 static inline _Self end (GraphT G) { return _Self(); }
49
50 inline bool operator==(const _Self& x) const {
51 return VisitStack == x.VisitStack;
52 }
53 inline bool operator!=(const _Self& x) const { return !operator==(x); }
54
55 inline pointer operator*() const {
56 return VisitStack.top().first;
57 }
58
59 // This is a nonstandard operator-> that dereferences the pointer an extra
60 // time... so that you can actually call methods ON the BasicBlock, because
61 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
62 //
63 inline NodeType *operator->() const { return operator*(); }
64
65 inline _Self& operator++() { // Preincrement
66 VisitStack.pop();
67 if (!VisitStack.empty())
68 traverseChild();
69 return *this;
70 }
71
72 inline _Self operator++(int) { // Postincrement
73 _Self tmp = *this; ++*this; return tmp;
74 }
75 };
76
77 // Provide global constructors that automatically figure out correct types...
78 //
79 template
80 po_iterator po_begin(T G) { return po_iterator::begin(G); }
81 template
82 po_iterator po_end (T G) { return po_iterator::end(G); }
83
84 // Provide global definitions of inverse post order iterators...
85 template
86 struct ipo_iterator : public po_iterator > {
87 ipo_iterator(const po_iterator > &V) :po_iterator >(V){}
88 };
89
90 template
91 ipo_iterator ipo_begin(T G, bool Reverse = false) {
92 return ipo_iterator::begin(G, Reverse);
93 }
94
95 template
96 ipo_iterator ipo_end(T G){
97 return ipo_iterator::end(G);
98 }
99
100
101 //===--------------------------------------------------------------------===//
102 // Reverse Post Order CFG iterator code
103 //===--------------------------------------------------------------------===//
104 //
105 // This is used to visit basic blocks in a method in reverse post order. This
106 // class is awkward to use because I don't know a good incremental algorithm to
107 // computer RPO from a graph. Because of this, the construction of the
108 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
109 // with a postorder iterator to build the data structures). The moral of this
110 // story is: Don't create more ReversePostOrderTraversal classes than neccesary.
111 //
112 // This class should be used like this:
113 // {
114 // cfg::ReversePostOrderTraversal RPOT(MethodPtr); // Expensive to create
115 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
116 // ...
117 // }
118 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
119 // ...
120 // }
121 // }
122 //
123
124 typedef reverse_iterator::iterator> rpo_iterator;
125 // TODO: FIXME: ReversePostOrderTraversal is not generic!
126 class ReversePostOrderTraversal {
127 vector Blocks; // Block list in normal PO order
128 inline void Initialize(BasicBlock *BB) {
129 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
130 }
131 public:
132 inline ReversePostOrderTraversal(Method *M) {
133 Initialize(M->front());
134 }
135 inline ReversePostOrderTraversal(BasicBlock *BB) {
136 Initialize(BB);
137 }
138
139 // Because we want a reverse post order, use reverse iterators from the vector
140 inline rpo_iterator begin() { return Blocks.rbegin(); }
141 inline rpo_iterator end() { return Blocks.rend(); }
142 };
143
144 #endif
+0
-222
include/llvm/Support/STLExtras.h less more
None //===-- STLExtras.h - Useful functions when working with the STL -*- C++ -*--=//
1 //
2 // This file contains some templates that are useful if you are working with the
3 // STL at all.
4 //
5 // No library is required when using these functinons.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_STL_EXTRAS_H
10 #define LLVM_SUPPORT_STL_EXTRAS_H
11
12 #include
13
14 //===----------------------------------------------------------------------===//
15 // Extra additions to
16 //===----------------------------------------------------------------------===//
17
18 // bind_obj - Often times you want to apply the member function of an object
19 // as a unary functor. This macro is shorthand that makes it happen less
20 // verbosely.
21 //
22 // Example:
23 // struct Summer { void accumulate(int x); }
24 // vector Numbers;
25 // Summer MyS;
26 // for_each(Numbers.begin(), Numbers.end(),
27 // bind_obj(&MyS, &Summer::accumulate));
28 //
29 // TODO: When I get lots of extra time, convert this from an evil macro
30 //
31 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
32
33
34 // bitwise_or - This is a simple functor that applys operator| on its two
35 // arguments to get a boolean result.
36 //
37 template
38 struct bitwise_or : public binary_function {
39 bool operator()(const Ty& left, const Ty& right) const {
40 return left | right;
41 }
42 };
43
44
45 // deleter - Very very very simple method that is used to invoke operator
46 // delete on something. It is used like this:
47 //
48 // for_each(V.begin(), B.end(), deleter);
49 //
50 template
51 static inline void deleter(T *Ptr) {
52 delete Ptr;
53 }
54
55
56
57 //===----------------------------------------------------------------------===//
58 // Extra additions to
59 //===----------------------------------------------------------------------===//
60
61 // mapped_iterator - This is a simple iterator adapter that causes a function to
62 // be dereferenced whenever operator* is invoked on the iterator.
63 //
64 // It turns out that this is disturbingly similar to boost::transform_iterator
65 //
66 #if 1
67 template
68 class mapped_iterator {
69 RootIt current;
70 UnaryFunc Fn;
71 public:
72 typedef typename iterator_traits::iterator_category
73 iterator_category;
74 typedef typename iterator_traits::difference_type
75 difference_type;
76 typedef typename UnaryFunc::result_type value_type;
77 typedef typename UnaryFunc::result_type *pointer;
78 typedef void reference; // Can't modify value returned by fn
79
80 typedef RootIt iterator_type;
81 typedef mapped_iterator _Self;
82
83 inline RootIt &getCurrent() const { return current; }
84
85 inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
86 : current(I), Fn(F) {}
87 inline mapped_iterator(const mapped_iterator &It)
88 : current(It.current), Fn(It.Fn) {}
89
90 inline value_type operator*() const { // All this work to do this
91 return Fn(*current); // little change
92 }
93
94 _Self& operator++() { ++current; return *this; }
95 _Self& operator--() { --current; return *this; }
96 _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
97 _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; }
98 _Self operator+ (difference_type n) const { return _Self(current + n); }
99 _Self& operator+= (difference_type n) { current += n; return *this; }
100 _Self operator- (difference_type n) const { return _Self(current - n); }
101 _Self& operator-= (difference_type n) { current -= n; return *this; }
102 reference operator[](difference_type n) const { return *(*this + n); }
103
104 inline bool operator==(const _Self &X) const { return current == X.current; }
105 inline bool operator< (const _Self &X) const { return current < X.current; }
106
107 inline difference_type operator-(const _Self &X) const {
108 return current - X.current;
109 }
110 };
111
112 template
113 inline mapped_iterator<_Iterator, Func>
114 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
115 const mapped_iterator<_Iterator, Func>& X) {
116 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
117 }
118
119 #else
120
121 // This fails to work, because some iterators are not classes, for example
122 // vector iterators are commonly value_type **'s
123 template
124 class mapped_iterator : public RootIt {
125 UnaryFunc Fn;
126 public:
127 typedef typename UnaryFunc::result_type value_type;
128 typedef typename UnaryFunc::result_type *pointer;
129 typedef void reference; // Can't modify value returned by fn
130
131 typedef mapped_iterator _Self;
132 typedef RootIt super;
133 inline explicit mapped_iterator(const RootIt &I) : super(I) {}
134 inline mapped_iterator(const super &It) : super(It) {}
135
136 inline value_type operator*() const { // All this work to do
137 return Fn(super::operator*()); // this little thing
138 }
139 };
140 #endif
141
142 // map_iterator - Provide a convenient way to create mapped_iterators, just like
143 // make_pair is useful for creating pairs...
144 //
145 template
146 inline mapped_iterator map_iterator(const ItTy &I, FuncTy F) {
147 return mapped_iterator(I, F);
148 }
149
150
151 //===----------------------------------------------------------------------===//
152 // Extra additions to
153 //===----------------------------------------------------------------------===//
154
155 // apply_until - Apply a functor to a sequence continually, unless the
156 // functor returns true. Return true if the functor returned true, return false
157 // if the functor never returned true.
158 //
159 template
160 bool apply_until(InputIt First, InputIt Last, Function Func) {
161 for ( ; First != Last; ++First)
162 if (Func(*First)) return true;
163 return false;
164 }
165
166
167 // reduce - Reduce a sequence values into a single value, given an initial
168 // value and an operator.
169 //
170 template
171 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
172 for ( ; First != Last; ++First)
173 Value = Func(*First, Value);
174 return Value;
175 }
176
177 #if 1 // This is likely to be more efficient
178
179 // reduce_apply - Reduce the result of applying a function to each value in a
180 // sequence, given an initial value, an operator, a function, and a sequence.
181 //
182 template
183 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func,
184 ValueType Value, TransFunc XForm) {
185 for ( ; First != Last; ++First)
186 Value = Func(XForm(*First), Value);
187 return Value;
188 }
189
190 #else // This is arguably more elegant
191
192 // reduce_apply - Reduce the result of applying a function to each value in a
193 // sequence, given an initial value, an operator, a function, and a sequence.
194 //
195 template
196 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func,
197 ValueType Value, TransFunc XForm) {
198 return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
199 Func, Value);
200 }
201 #endif
202
203
204 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
205 // to each value in a sequence. All of the bools returned by the mapped
206 // function are bitwise or'd together, and the result is returned.
207 //
208 template
209 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
210 return reduce_apply(First, Last, bitwise_or(), false, Func);
211 }
212
213
214 // map - This function maps the specified input sequence into the specified
215 // output iterator, applying a unary function in between.
216 //
217 template
218 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
219 return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
220 }
221 #endif
+0
-88
include/llvm/Support/StringExtras.h less more
None //===-- StringExtras.h - Useful string functions -----------------*- C++ -*--=//
1 //
2 // This file contains some functions that are useful when dealing with strings.
3 //
4 //===----------------------------------------------------------------------===//
5
6 #ifndef LLVM_TOOLS_STRING_EXTRAS_H
7 #define LLVM_TOOLS_STRING_EXTRAS_H
8
9 #include
10 #include
11 #include "Support/DataTypes.h"
12
13 class ConstPoolArray;
14
15 static inline string utostr(uint64_t X, bool isNeg = false) {
16 char Buffer[40];
17 char *BufPtr = Buffer+39;
18
19 *BufPtr = 0; // Null terminate buffer...
20 if (X == 0) *--BufPtr = '0'; // Handle special case...
21
22 while (X) {
23 *--BufPtr = '0' + (X % 10);
24 X /= 10;
25 }
26
27 if (isNeg) *--BufPtr = '-'; // Add negative sign...
28
29 return string(BufPtr);
30 }
31
32 static inline string itostr(int64_t X) {
33 if (X < 0)
34 return utostr((uint64_t)-X, true);
35 else
36 return utostr((uint64_t)X);
37 }
38
39
40 static inline string utostr(unsigned X, bool isNeg = false) {
41 char Buffer[20];
42 char *BufPtr = Buffer+19;
43
44 *BufPtr = 0; // Null terminate buffer...
45 if (X == 0) *--BufPtr = '0'; // Handle special case...
46
47 while (X) {
48 *--BufPtr = '0' + (X % 10);
49 X /= 10;
50 }
51
52 if (isNeg) *--BufPtr = '-'; // Add negative sign...
53
54 return string(BufPtr);
55 }
56
57 static inline string itostr(int X) {
58 if (X < 0)
59 return utostr((unsigned)-X, true);
60 else
61 return utostr((unsigned)X);
62 }
63
64 static inline string ftostr(double V) {
65 char Buffer[200];
66 snprintf(Buffer, 200, "%e", V);
67 return Buffer;
68 }
69
70 static inline void
71 printIndent(unsigned int indent, ostream& os=cout, const char* const istr=" ")
72 {
73 for (unsigned i=0; i < indent; i++)
74 os << istr;
75 }
76
77 // Can we treat the specified array as a string? Only if it is an array of
78 // ubytes or non-negative sbytes.
79 //
80 bool isStringCompatible(ConstPoolArray *CPA);
81
82 // getAsCString - Return the specified array as a C compatible string, only if
83 // the predicate isStringCompatible is true.
84 //
85 string getAsCString(ConstPoolArray *CPA);
86
87 #endif
+0
-52
include/llvm/Support/Tree.h less more
None //===- llvm/Support/Tree.h - Generic n-way tree structure --------*- C++ -*--=//
1 //
2 // This class defines a generic N way tree node structure. The tree structure
3 // is immutable after creation, but the payload contained within it is not.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_SUPPORT_TREE_H
8 #define LLVM_SUPPORT_TREE_H
9
10 #include
11
12 template
13 class Tree {
14 vector Children; // This nodes children, if any
15 ConcreteTreeNode *Parent; // Parent of this node...
16 Payload Data; // Data held in this node...
17
18 protected:
19 void setChildren(const vector &children) {
20 Children = children;
21 }
22 public:
23 inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
24 inline Tree(const vector &children, ConcreteTreeNode *par)
25 : Children(children), Parent(par) {}
26
27 inline Tree(const vector &children, ConcreteTreeNode *par,
28 const Payload &data)
29 : Children(children), Parent(parent), Data(data) {}
30
31 // Tree dtor - Free all children
32 inline ~Tree() {
33 for (unsigned i = Children.size(); i > 0; --i)
34 delete Children[i-1];
35 }
36
37 // Tree manipulation/walking routines...
38 inline ConcreteTreeNode *getParent() const { return Parent; }
39 inline unsigned getNumChildren() const { return Children.size(); }
40 inline ConcreteTreeNode *getChild(unsigned i) const {
41 assert(i < Children.size() && "Tree::getChild with index out of range!");
42 return Children[i];
43 }
44
45 // Payload access...
46 inline Payload &getTreeData() { return Data; }
47 inline const Payload &getTreeData() const { return Data; }
48 };
49
50
51 #endif
1212 #ifndef LLVM_CODEGEN_FRAMEINFO_H
1313 #define LLVM_CODEGEN_FRAMEINFO_H
1414
15 #include "llvm/Support/NonCopyable.h"
15 #include "Support/NonCopyable.h"
1616 #include
1717
1818
77 #define LLVM_TARGET_TARGETMACHINE_H
88
99 #include "llvm/Target/TargetData.h"
10 #include "llvm/Support/NonCopyable.h"
10 #include "Support/NonCopyable.h"
1111
1212 class TargetMachine;
1313 class MachineInstrInfo;
77 #ifndef LLVM_TARGET_MACHINEREGINFO_H
88 #define LLVM_TARGET_MACHINEREGINFO_H
99
10 #include "llvm/Support/NonCopyable.h"
10 #include "Support/NonCopyable.h"
1111 #include
1212 #include
1313
2626 #define LLVM_TYPE_H
2727
2828 #include "llvm/Value.h"
29 #include "llvm/Support/GraphTraits.h"
29 #include "Support/GraphTraits.h"
3030
3131 class DerivedType;
3232 class MethodType;
1111
1212 #include "llvm/Analysis/CallGraph.h"
1313 #include "llvm/Analysis/Writer.h"
14 #include "llvm/Support/STLExtras.h"
1514 #include "llvm/Module.h"
1615 #include "llvm/Method.h"
1716 #include "llvm/iOther.h"
1817 #include "llvm/iTerminators.h"
18 #include "Support/STLExtras.h"
1919 #include
2020
2121 // getNodeFor - Return the node for the specified method or create one if it
1717
1818 #include "llvm/Analysis/FindUnsafePointerTypes.h"
1919 #include "llvm/Assembly/CachedWriter.h"
20 #include "llvm/Support/CommandLine.h"
2120 #include "llvm/Type.h"
21 #include "Support/CommandLine.h"
2222
2323 // Provide a command line option to turn on printing of which instructions cause
2424 // a type to become invalid
55 //===----------------------------------------------------------------------===//
66
77 #include "llvm/Analysis/IntervalIterator.h"
8 #include "llvm/Support/STLExtras.h"
8 #include "Support/STLExtras.h"
99
1010 using namespace cfg;
1111
1010
1111 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
1212 #include "llvm/CodeGen/MachineInstr.h"
13 #include "llvm/Support/PostOrderIterator.h"
13 #include "Support/PostOrderIterator.h"
1414
1515
1616 /************************** Constructor/Destructor ***************************/
66
77 #include "llvm/Analysis/LoopDepth.h"
88 #include "llvm/Analysis/IntervalPartition.h"
9 #include "llvm/Support/STLExtras.h"
9 #include "Support/STLExtras.h"
1010 #include
1111
1212 inline void LoopDepthCalculator::AddBB(const BasicBlock *BB) {
88
99 #include "llvm/Analysis/LoopInfo.h"
1010 #include "llvm/Analysis/Dominators.h"
11 #include "llvm/Support/DepthFirstIterator.h"
1211 #include "llvm/BasicBlock.h"
12 #include "Support/DepthFirstIterator.h"
1313 #include
1414
1515 bool cfg::Loop::contains(const BasicBlock *BB) const {
1111 #include "llvm/BasicBlock.h"
1212 #include "llvm/DerivedTypes.h"
1313 #include "llvm/ConstPoolVals.h"
14 #include "llvm/Support/STLExtras.h"
14 #include "Support/STLExtras.h"
1515 #include
1616
1717 // processModule - Driver function to call all of my subclasses virtual methods.
55
66 #include "llvm/Analysis/Dominators.h"
77 #include "llvm/Analysis/SimplifyCFG.h" // To get cfg::UnifyAllExitNodes
8 #include "llvm/Support/DepthFirstIterator.h"
9 #include "llvm/Support/STLExtras.h"
108 #include "llvm/Method.h"
9 #include "Support/DepthFirstIterator.h"
10 #include "Support/STLExtras.h"
1111 #include
1212
1313 //===----------------------------------------------------------------------===//
1717 #include "llvm/Method.h"
1818 #include "llvm/DerivedTypes.h"
1919 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Support/StringExtras.h"
20 #include "Support/StringExtras.h"
2121
2222 class Module;
2323
2020 #include "llvm/DerivedTypes.h"
2121 #include "llvm/iTerminators.h"
2222 #include "llvm/iMemory.h"
23 #include "llvm/Support/STLExtras.h"
24 #include "llvm/Support/DepthFirstIterator.h"
23 #include "Support/STLExtras.h"
24 #include "Support/DepthFirstIterator.h"
2525 #include
2626 #include // Get definition of pair class
2727 #include
1818 #include "llvm/iOther.h"
1919 #include "llvm/DerivedTypes.h"
2020 #include "llvm/SymbolTable.h"
21 #include "llvm/Support/STLExtras.h"
22 #include "llvm/Support/DepthFirstIterator.h"
21 #include "Support/DepthFirstIterator.h"
22 #include "Support/STLExtras.h"
2323 #include
2424
2525 #if 0
1212 //************************* User Include Files *****************************/
1313
1414 #include "llvm/CodeGen/InstrScheduling.h"
15 #include "SchedPriorities.h"
1615 #include "llvm/Analysis/LiveVar/BBLiveVar.h"
1716 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/Support/CommandLine.h"
1917 #include "llvm/Instruction.h"
20
21
22 //************************ System Include Files *****************************/
23
18 #include "Support/CommandLine.h"
19 #include "SchedPriorities.h"
2420 #include
2521 #include
2622 #include
2020 #include "llvm/CodeGen/InstrSelection.h"
2121 #include "llvm/Target/MachineInstrInfo.h"
2222 #include "llvm/Target/MachineRegInfo.h"
23 #include "llvm/Support/StringExtras.h"
2423 #include "llvm/iOther.h"
24 #include "Support/StringExtras.h"
2525 #include
2626 #include
2727 #include
131131 }
132132
133133 void SchedGraphEdge::dump(int indent=0) const {
134 printIndent(indent); cout << *this;
134 cout << string(indent*2, ' ') << *this;
135135 }
136136
137137
167167 }
168168
169169 void SchedGraphNode::dump(int indent=0) const {
170 printIndent(indent); cout << *this;
170 cout << string(indent*2, ' ') << *this;
171171 }
172172
173173
10221022 ostream&
10231023 operator<<(ostream& os, const SchedGraphNode& node)
10241024 {
1025 printIndent(4, os);
1026 os << "Node " << node.nodeId << " : "
1027 << "latency = " << node.latency << endl;
1028
1029 printIndent(6, os);
1025 os << string(8, ' ')
1026 << "Node " << node.nodeId << " : "
1027 << "latency = " << node.latency << endl << string(12, ' ');
10301028
10311029 if (node.getMachineInstr() == NULL)
10321030 os << "(Dummy node)" << endl;
10331031 else
10341032 {
1035 os << *node.getMachineInstr() << endl;
1036
1037 printIndent(6, os);
1033 os << *node.getMachineInstr() << endl << string(12, ' ');
10381034 os << node.inEdges.size() << " Incoming Edges:" << endl;
10391035 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
1040 {
1041 printIndent(8, os);
1042 os << * node.inEdges[i];
1043 }
1044
1045 printIndent(6, os);
1046 os << node.outEdges.size() << " Outgoing Edges:" << endl;
1036 os << string(16, ' ') << *node.inEdges[i];
1037
1038 os << string(12, ' ') << node.outEdges.size()
1039 << " Outgoing Edges:" << endl;
10471040 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
10481041