annotate gen/passes/GarbageCollect2Stack.cpp @ 1438:c363d131c1ef

Add some missing returns.
author Frits van Bommel <fvbommel wxs.nl>
date Sat, 30 May 2009 23:48:22 +0200
parents ad999630aa35
children 9ed0695cb93c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
1 //===- GarbageCollect2Stack - Optimize calls to the D garbage collector ---===//
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
2 //
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
3 // The LLVM D Compiler
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
4 //
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
7 //
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
9 //
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
10 // This file attempts to turn allocations on the garbage-collected heap into
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
11 // stack allocations.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
12 //
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
14
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
15 #include "gen/metadata.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
16
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
17 // This pass doesn't work without metadata, so #ifdef it out entirely if the
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
18 // LLVM version in use doesn't support it.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
19 #ifdef USE_METADATA
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
20
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
21
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
22 #define DEBUG_TYPE "dgc2stack"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
23
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
24 #include "Passes.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
25
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
26 #include "llvm/Pass.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
27 #include "llvm/Module.h"
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
28 #include "llvm/Intrinsics.h"
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
29 #include "llvm/Support/CallSite.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
30 #include "llvm/Support/CommandLine.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
31 #include "llvm/Support/IRBuilder.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
32 #include "llvm/Analysis/CaptureTracking.h"
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
33 #include "llvm/Analysis/CallGraph.h"
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
34 #include "llvm/Analysis/ValueTracking.h"
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
35 #include "llvm/Analysis/LoopInfo.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
36 #include "llvm/Target/TargetData.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
37 #include "llvm/ADT/StringMap.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
38 #include "llvm/ADT/Statistic.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
39 #include "llvm/Support/Compiler.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
40 #include "llvm/Support/Debug.h"
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
41 using namespace llvm;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
42
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
43 STATISTIC(NumGcToStack, "Number of calls promoted to constant-size allocas");
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
44 STATISTIC(NumToDynSize, "Number of calls promoted to dynamically-sized allocas");
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
45 STATISTIC(NumDeleted, "Number of GC calls deleted because the return value was unused");
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
46
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
47
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
48 namespace {
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
49 struct Analysis {
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
50 TargetData& TD;
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
51 const Module& M;
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
52 CallGraph* CG;
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
53 CallGraphNode* CGNode;
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
54
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
55 const Type* getTypeFor(Value* typeinfo) const;
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
56 };
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
57 }
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
58
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
59 //===----------------------------------------------------------------------===//
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
60 // Helper functions
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
61 //===----------------------------------------------------------------------===//
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
62
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
63 void EmitMemSet(IRBuilder<>& B, Value* Dst, Value* Val, Value* Len,
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
64 const Analysis& A) {
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
65 Dst = B.CreateBitCast(Dst, PointerType::getUnqual(Type::Int8Ty));
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
66
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
67 Module *M = B.GetInsertBlock()->getParent()->getParent();
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
68 const Type* Tys[1];
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
69 Tys[0] = Len->getType();
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
70 Function *MemSet = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys, 1);
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
71 Value *Align = ConstantInt::get(Type::Int32Ty, 1);
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
72
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
73 CallSite CS = B.CreateCall4(MemSet, Dst, Val, Len, Align);
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
74 if (A.CGNode)
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
75 A.CGNode->addCalledFunction(CS, A.CG->getOrInsertFunction(MemSet));
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
76 }
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
77
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
78 static void EmitMemZero(IRBuilder<>& B, Value* Dst, Value* Len,
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
79 const Analysis& A) {
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
80 EmitMemSet(B, Dst, ConstantInt::get(Type::Int8Ty, 0), Len, A);
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
81 }
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
82
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
83
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
84 //===----------------------------------------------------------------------===//
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
85 // Helpers for specific types of GC calls.
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
86 //===----------------------------------------------------------------------===//
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
87
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
88 namespace {
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
89 class FunctionInfo {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
90 protected:
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
91 const Type* Ty;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
92
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
93 public:
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
94 unsigned TypeInfoArgNr;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
95 bool SafeToDelete;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
96
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
97 // Analyze the current call, filling in some fields. Returns true if
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
98 // this is an allocation we can stack-allocate.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
99 virtual bool analyze(CallSite CS, const Analysis& A) {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
100 Value* TypeInfo = CS.getArgument(TypeInfoArgNr);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
101 Ty = A.getTypeFor(TypeInfo);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
102 return (Ty != NULL);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
103 }
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
104
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
105 // Returns the alloca to replace this call.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
106 // It will always be inserted before the call.
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
107 virtual AllocaInst* promote(CallSite CS, IRBuilder<>& B, const Analysis& A) {
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
108 NumGcToStack++;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
109
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
110 Instruction* Begin = CS.getCaller()->getEntryBlock().begin();
1350
15e9762bb620 Adds explicit alignment information for alloca instructions in general, there's a few cases that still needs to be looked at but this should catch the majority. Fixes ticket #293 .
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents: 1344
diff changeset
111 return new AllocaInst(Ty, ".nongc_mem", Begin); // FIXME: align?
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
112 }
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
113
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
114 FunctionInfo(unsigned typeInfoArgNr, bool safeToDelete)
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
115 : TypeInfoArgNr(typeInfoArgNr), SafeToDelete(safeToDelete) {}
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
116 };
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
117
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
118 class ArrayFI : public FunctionInfo {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
119 Value* arrSize;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
120 int ArrSizeArgNr;
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
121 bool Initialized;
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
122
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
123 public:
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
124 ArrayFI(unsigned tiArgNr, bool safeToDelete, bool initialized,
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
125 unsigned arrSizeArgNr)
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
126 : FunctionInfo(tiArgNr, safeToDelete),
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
127 ArrSizeArgNr(arrSizeArgNr),
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
128 Initialized(initialized)
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
129 {}
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
130
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
131 virtual bool analyze(CallSite CS, const Analysis& A) {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
132 if (!FunctionInfo::analyze(CS, A))
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
133 return false;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
134
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
135 arrSize = CS.getArgument(ArrSizeArgNr);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
136 const IntegerType* SizeType =
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
137 dyn_cast<IntegerType>(arrSize->getType());
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
138 if (!SizeType)
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
139 return false;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
140 unsigned bits = SizeType->getBitWidth();
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
141 if (bits > 32) {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
142 // The array size of an alloca must be an i32, so make sure
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
143 // the conversion is safe.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
144 APInt Mask = APInt::getHighBitsSet(bits, bits - 32);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
145 APInt KnownZero(bits, 0), KnownOne(bits, 0);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
146 ComputeMaskedBits(arrSize, Mask, KnownZero, KnownOne, &A.TD);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
147 if ((KnownZero & Mask) != Mask)
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
148 return false;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
149 }
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
150 // Extract the element type from the array type.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
151 const StructType* ArrTy = dyn_cast<StructType>(Ty);
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
152 assert(ArrTy && "Dynamic array type not a struct?");
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
153 assert(isa<IntegerType>(ArrTy->getElementType(0)));
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
154 const PointerType* PtrTy =
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
155 cast<PointerType>(ArrTy->getElementType(1));
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
156 Ty = PtrTy->getElementType();
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
157 return true;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
158 }
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
159
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
160 virtual AllocaInst* promote(CallSite CS, IRBuilder<>& B, const Analysis& A) {
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
161 IRBuilder<> Builder = B;
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
162 // If the allocation is of constant size it's best to put it in the
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
163 // entry block, so do so if we're not already there.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
164 // For dynamically-sized allocations it's best to avoid the overhead
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
165 // of allocating them if possible, so leave those where they are.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
166 // While we're at it, update statistics too.
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
167 if (isa<Constant>(arrSize)) {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
168 BasicBlock& Entry = CS.getCaller()->getEntryBlock();
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
169 if (Builder.GetInsertBlock() != &Entry)
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
170 Builder.SetInsertPoint(&Entry, Entry.begin());
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
171 NumGcToStack++;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
172 } else {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
173 NumToDynSize++;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
174 }
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
175
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
176 // Convert array size to 32 bits if necessary
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
177 Value* count = Builder.CreateIntCast(arrSize, Type::Int32Ty, false);
1350
15e9762bb620 Adds explicit alignment information for alloca instructions in general, there's a few cases that still needs to be looked at but this should catch the majority. Fixes ticket #293 .
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents: 1344
diff changeset
178 AllocaInst* alloca = Builder.CreateAlloca(Ty, count, ".nongc_mem"); // FIXME: align?
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
179
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
180 if (Initialized) {
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
181 // For now, only zero-init is supported.
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
182 uint64_t size = A.TD.getTypeStoreSize(Ty);
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
183 Value* TypeSize = ConstantInt::get(arrSize->getType(), size);
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
184 // Use the original B to put initialization at the
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
185 // allocation site.
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
186 Value* Size = B.CreateMul(TypeSize, arrSize);
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
187 EmitMemZero(B, alloca, Size, A);
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
188 }
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
189
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
190 return alloca;
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
191 }
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
192 };
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
193
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
194 // FunctionInfo for _d_allocclass
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
195 class AllocClassFI : public FunctionInfo {
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
196 public:
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
197 virtual bool analyze(CallSite CS, const Analysis& A) {
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
198 // This call contains no TypeInfo parameter, so don't call the
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
199 // base class implementation here...
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
200 if (CS.arg_size() != 1)
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
201 return false;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
202 Value* arg = CS.getArgument(0)->stripPointerCasts();
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
203 GlobalVariable* ClassInfo = dyn_cast<GlobalVariable>(arg);
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
204 if (!ClassInfo)
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
205 return false;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
206
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
207 std::string metaname = CD_PREFIX;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
208 metaname.append(ClassInfo->getNameStart(), ClassInfo->getNameEnd());
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
209
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
210 GlobalVariable* global = A.M.getGlobalVariable(metaname);
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
211 if (!global || !global->hasInitializer())
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
212 return false;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
213
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
214 MDNode* node = dyn_cast<MDNode>(global->getInitializer());
1343
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
215 if (!node || MD_GetNumElements(node) != CD_NumFields)
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
216 return false;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
217
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
218 // Inserting destructor calls is not implemented yet, so classes
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
219 // with destructors are ignored for now.
1343
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
220 Constant* hasDestructor = dyn_cast<Constant>(MD_GetElement(node, CD_Finalize));
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
221 // We can't stack-allocate if the class has a custom deallocator
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
222 // (Custom allocators don't get turned into this runtime call, so
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
223 // those can be ignored)
1343
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
224 Constant* hasCustomDelete = dyn_cast<Constant>(MD_GetElement(node, CD_CustomDelete));
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
225 if (hasDestructor == NULL || hasCustomDelete == NULL)
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
226 return false;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
227
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
228 if (ConstantExpr::getOr(hasDestructor, hasCustomDelete)
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
229 != ConstantInt::getFalse())
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
230 return false;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
231
1343
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
232 Ty = MD_GetElement(node, CD_BodyType)->getType();
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
233 return true;
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
234 }
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
235
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
236 // The default promote() should be fine.
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
237
1361
67ac63740c7f silence a gcc warning
Benjamin Kramer <benny.kra@gmail.com>
parents: 1350
diff changeset
238 AllocClassFI() : FunctionInfo(~0u, true) {}
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
239 };
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
240 }
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
241
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
242
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
243 //===----------------------------------------------------------------------===//
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
244 // GarbageCollect2Stack Pass Implementation
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
245 //===----------------------------------------------------------------------===//
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
246
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
247 namespace {
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
248 /// This pass replaces GC calls with alloca's
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
249 ///
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
250 class VISIBILITY_HIDDEN GarbageCollect2Stack : public FunctionPass {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
251 StringMap<FunctionInfo*> KnownFunctions;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
252 Module* M;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
253
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
254 FunctionInfo AllocMemoryT;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
255 ArrayFI NewArrayVT;
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
256 ArrayFI NewArrayT;
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
257 AllocClassFI AllocClass;
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
258
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
259 public:
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
260 static char ID; // Pass identification
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
261 GarbageCollect2Stack();
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
262
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
263 bool doInitialization(Module &M) {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
264 this->M = &M;
1438
c363d131c1ef Add some missing returns.
Frits van Bommel <fvbommel wxs.nl>
parents: 1424
diff changeset
265 return false;
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
266 }
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
267
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
268 bool runOnFunction(Function &F);
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
269
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
270 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
271 AU.addRequired<TargetData>();
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
272 AU.addRequired<LoopInfo>();
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
273 AU.addPreserved<CallGraph>();
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
274 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
275 };
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
276 char GarbageCollect2Stack::ID = 0;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
277 } // end anonymous namespace.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
278
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
279 static RegisterPass<GarbageCollect2Stack>
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
280 X("dgc2stack", "Promote (GC'ed) heap allocations to stack");
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
281
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
282 // Public interface to the pass.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
283 FunctionPass *createGarbageCollect2Stack() {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
284 return new GarbageCollect2Stack();
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
285 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
286
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
287 GarbageCollect2Stack::GarbageCollect2Stack()
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
288 : FunctionPass(&ID),
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
289 AllocMemoryT(0, true),
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
290 NewArrayVT(0, true, false, 1),
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
291 NewArrayT(0, true, true, 1)
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
292 {
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
293 KnownFunctions["_d_allocmemoryT"] = &AllocMemoryT;
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
294 KnownFunctions["_d_newarrayvT"] = &NewArrayVT;
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
295 KnownFunctions["_d_newarrayT"] = &NewArrayT;
1317
4099548c80e0 Allocate objects on the stack if they (a) don't have a destructor, and
Frits van Bommel <fvbommel wxs.nl>
parents: 1316
diff changeset
296 KnownFunctions["_d_allocclass"] = &AllocClass;
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
297 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
298
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
299 static void RemoveCall(CallSite CS, const Analysis& A) {
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
300 if (CS.isInvoke()) {
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
301 InvokeInst* Invoke = cast<InvokeInst>(CS.getInstruction());
1298
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
302 // If this was an invoke instruction, we need to do some extra
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
303 // work to preserve the control flow.
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
304
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
305 // First notify the exception landing pad block that we won't be
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
306 // going there anymore.
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
307 Invoke->getUnwindDest()->removePredecessor(Invoke->getParent());
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
308 // Create a branch to the "normal" destination.
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
309 BranchInst::Create(Invoke->getNormalDest(), Invoke->getParent());
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
310 }
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
311 // Remove the runtime call.
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
312 if (A.CGNode)
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
313 A.CGNode->removeCallEdgeFor(CS);
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
314 CS.getInstruction()->eraseFromParent();
1298
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
315 }
7e303f9f16c7 Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents: 1297
diff changeset
316
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
317 /// runOnFunction - Top level algorithm.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
318 ///
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
319 bool GarbageCollect2Stack::runOnFunction(Function &F) {
1295
0e79fb40c4d0 Remove some overly verbose debug output
Frits van Bommel <fvbommel wxs.nl>
parents: 1291
diff changeset
320 DEBUG(DOUT << "Running -dgc2stack on function " << F.getName() << '\n');
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
321
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
322 TargetData &TD = getAnalysis<TargetData>();
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
323 const LoopInfo &LI = getAnalysis<LoopInfo>();
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
324 CallGraph* CG = getAnalysisIfAvailable<CallGraph>();
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
325 CallGraphNode* CGNode = CG ? (*CG)[&F] : NULL;
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
326
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
327 Analysis A = { TD, *M, CG, CGNode };
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
328
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
329 BasicBlock& Entry = F.getEntryBlock();
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
330
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
331 IRBuilder<> AllocaBuilder(&Entry, Entry.begin());
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
332
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
333 bool Changed = false;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
334 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
335 // We don't yet have sufficient analysis to properly determine if
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
336 // allocations will be unreferenced when the loop returns to their
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
337 // allocation point, so we're playing it safe by ignoring allocations
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
338 // in loops.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
339 // TODO: Analyze loops too...
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
340 if (LI.getLoopFor(BB)) {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
341 continue;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
342 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
343
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
344 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
345 // Ignore non-calls.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
346 Instruction* Inst = I++;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
347 CallSite CS = CallSite::get(Inst);
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
348 if (!CS.getInstruction())
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
349 continue;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
350
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
351 // Ignore indirect calls and calls to non-external functions.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
352 Function *Callee = CS.getCalledFunction();
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
353 if (Callee == 0 || !Callee->isDeclaration() ||
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
354 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
355 continue;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
356
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
357 // Ignore unknown calls.
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
358 const char *CalleeName = Callee->getNameStart();
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
359 StringMap<FunctionInfo*>::iterator OMI =
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
360 KnownFunctions.find(CalleeName, CalleeName+Callee->getNameLen());
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
361 if (OMI == KnownFunctions.end()) continue;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
362
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
363 assert(isa<PointerType>(Inst->getType())
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
364 && "GC function doesn't return a pointer?");
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
365
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
366 FunctionInfo* info = OMI->getValue();
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
367
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
368 if (Inst->use_empty() && info->SafeToDelete) {
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
369 Changed = true;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
370 NumDeleted++;
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
371 RemoveCall(CS, A);
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
372 continue;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
373 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
374
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
375 DEBUG(DOUT << "GarbageCollect2Stack inspecting: " << *Inst);
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
376
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
377 if (!info->analyze(CS, A) || PointerMayBeCaptured(Inst, true))
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
378 continue;
1305
8215dbf0e09f Postpone (expensive) escape analysis until we're sure it's needed.
Frits van Bommel <fvbommel wxs.nl>
parents: 1298
diff changeset
379
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
380 // Let's alloca this!
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
381 Changed = true;
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
382
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
383 IRBuilder<> Builder(BB, Inst);
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
384 Value* newVal = info->promote(CS, Builder, A);
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
385
1343
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
386 DEBUG(DOUT << "Promoted to: " << *newVal);
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
387
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
388 // Make sure the type is the same as it was before, and replace all
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
389 // uses of the runtime call with the alloca.
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
390 if (newVal->getType() != Inst->getType())
1307
e2ec50329af1 Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents: 1306
diff changeset
391 newVal = Builder.CreateBitCast(newVal, Inst->getType());
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
392 Inst->replaceAllUsesWith(newVal);
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
393
1424
ad999630aa35 Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents: 1361
diff changeset
394 RemoveCall(CS, A);
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
395 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
396 }
1297
8e8552601ecd Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents: 1295
diff changeset
397
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
398 return Changed;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
399 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
400
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
401 const Type* Analysis::getTypeFor(Value* typeinfo) const {
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
402 GlobalVariable* ti_global = dyn_cast<GlobalVariable>(typeinfo->stripPointerCasts());
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
403 if (!ti_global)
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
404 return NULL;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
405
1291
875afb7a93b6 Factor out some constants into the header so producers and consumers of
Frits van Bommel <fvbommel wxs.nl>
parents: 1285
diff changeset
406 std::string metaname = TD_PREFIX;
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
407 metaname.append(ti_global->getNameStart(), ti_global->getNameEnd());
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
408
1306
b61db48127fd Some refactoring
Frits van Bommel <fvbommel wxs.nl>
parents: 1305
diff changeset
409 GlobalVariable* global = M.getGlobalVariable(metaname);
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
410 if (!global || !global->hasInitializer())
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
411 return NULL;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
412
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
413 MDNode* node = dyn_cast<MDNode>(global->getInitializer());
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
414 if (!node)
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
415 return NULL;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
416
1344
3297edb697eb Re-enable consistency check for fixed LLVM versions.
Frits van Bommel <fvbommel wxs.nl>
parents: 1343
diff changeset
417 if (MD_GetNumElements(node) != TD_NumFields)
3297edb697eb Re-enable consistency check for fixed LLVM versions.
Frits van Bommel <fvbommel wxs.nl>
parents: 1343
diff changeset
418 return NULL;
3297edb697eb Re-enable consistency check for fixed LLVM versions.
Frits van Bommel <fvbommel wxs.nl>
parents: 1343
diff changeset
419 if (TD_Confirm >= 0 && (!MD_GetElement(node, TD_Confirm) ||
3297edb697eb Re-enable consistency check for fixed LLVM versions.
Frits van Bommel <fvbommel wxs.nl>
parents: 1343
diff changeset
420 MD_GetElement(node, TD_Confirm)->stripPointerCasts() != ti_global))
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
421 return NULL;
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
422
1343
c21a6654cce2 Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents: 1317
diff changeset
423 return MD_GetElement(node, TD_Type)->getType();
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
424 }
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
425
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
426
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
427 #endif //USE_METADATA