Mercurial > projects > ldc
annotate gen/passes/GarbageCollect2Stack.cpp @ 1488:ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Mon, 08 Jun 2009 13:52:45 +0200 |
parents | 9ed0695cb93c |
children | 360a8e8eea51 |
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" |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
32 #include "llvm/Analysis/CallGraph.h" |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
33 #include "llvm/Analysis/Dominators.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/Target/TargetData.h" |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
36 #include "llvm/ADT/SmallSet.h" |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
37 #include "llvm/ADT/SmallVector.h" |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
38 #include "llvm/ADT/StringMap.h" |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
39 #include "llvm/ADT/Statistic.h" |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
40 #include "llvm/Support/Compiler.h" |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
41 #include "llvm/Support/Debug.h" |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
42 using namespace llvm; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
43 |
1297
8e8552601ecd
Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents:
1295
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
48 |
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
49 namespace { |
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 |
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
56 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
|
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 } |
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
59 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
60 //===----------------------------------------------------------------------===// |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
61 // Helper functions |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
62 //===----------------------------------------------------------------------===// |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
63 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
64 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
|
65 const Analysis& A) { |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
66 Dst = B.CreateBitCast(Dst, PointerType::getUnqual(Type::Int8Ty)); |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
67 |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
68 Module *M = B.GetInsertBlock()->getParent()->getParent(); |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
69 const Type* Tys[1]; |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
70 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
|
71 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
|
72 Value *Align = ConstantInt::get(Type::Int32Ty, 1); |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
73 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 } |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
78 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
79 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
|
80 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
|
81 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
|
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 //===----------------------------------------------------------------------===// |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
86 // Helpers for specific types of GC calls. |
1285
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 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
89 namespace { |
1306 | 90 class FunctionInfo { |
91 protected: | |
92 const Type* Ty; | |
93 | |
94 public: | |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
95 unsigned TypeInfoArgNr; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
96 bool SafeToDelete; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
97 |
1306 | 98 // Analyze the current call, filling in some fields. Returns true if |
99 // this is an allocation we can stack-allocate. | |
100 virtual bool analyze(CallSite CS, const Analysis& A) { | |
101 Value* TypeInfo = CS.getArgument(TypeInfoArgNr); | |
102 Ty = A.getTypeFor(TypeInfo); | |
103 return (Ty != NULL); | |
104 } | |
105 | |
106 // Returns the alloca to replace this call. | |
107 // 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
|
108 virtual AllocaInst* promote(CallSite CS, IRBuilder<>& B, const Analysis& A) { |
1306 | 109 NumGcToStack++; |
110 | |
111 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
|
112 return new AllocaInst(Ty, ".nongc_mem", Begin); // FIXME: align? |
1306 | 113 } |
114 | |
115 FunctionInfo(unsigned typeInfoArgNr, bool safeToDelete) | |
116 : TypeInfoArgNr(typeInfoArgNr), SafeToDelete(safeToDelete) {} | |
117 }; | |
118 | |
119 class ArrayFI : public FunctionInfo { | |
120 Value* arrSize; | |
121 int ArrSizeArgNr; | |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
122 bool Initialized; |
1306 | 123 |
124 public: | |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
125 ArrayFI(unsigned tiArgNr, bool safeToDelete, bool initialized, |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
126 unsigned arrSizeArgNr) |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
127 : FunctionInfo(tiArgNr, safeToDelete), |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
128 ArrSizeArgNr(arrSizeArgNr), |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
129 Initialized(initialized) |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
130 {} |
1306 | 131 |
132 virtual bool analyze(CallSite CS, const Analysis& A) { | |
133 if (!FunctionInfo::analyze(CS, A)) | |
134 return false; | |
135 | |
136 arrSize = CS.getArgument(ArrSizeArgNr); | |
137 const IntegerType* SizeType = | |
138 dyn_cast<IntegerType>(arrSize->getType()); | |
139 if (!SizeType) | |
140 return false; | |
141 unsigned bits = SizeType->getBitWidth(); | |
142 if (bits > 32) { | |
143 // The array size of an alloca must be an i32, so make sure | |
144 // the conversion is safe. | |
145 APInt Mask = APInt::getHighBitsSet(bits, bits - 32); | |
146 APInt KnownZero(bits, 0), KnownOne(bits, 0); | |
147 ComputeMaskedBits(arrSize, Mask, KnownZero, KnownOne, &A.TD); | |
148 if ((KnownZero & Mask) != Mask) | |
149 return false; | |
150 } | |
151 // Extract the element type from the array type. | |
152 const StructType* ArrTy = dyn_cast<StructType>(Ty); | |
153 assert(ArrTy && "Dynamic array type not a struct?"); | |
154 assert(isa<IntegerType>(ArrTy->getElementType(0))); | |
155 const PointerType* PtrTy = | |
156 cast<PointerType>(ArrTy->getElementType(1)); | |
157 Ty = PtrTy->getElementType(); | |
158 return true; | |
159 } | |
160 | |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
161 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
|
162 IRBuilder<> Builder = B; |
1306 | 163 // If the allocation is of constant size it's best to put it in the |
164 // entry block, so do so if we're not already there. | |
165 // For dynamically-sized allocations it's best to avoid the overhead | |
166 // of allocating them if possible, so leave those where they are. | |
167 // While we're at it, update statistics too. | |
168 if (isa<Constant>(arrSize)) { | |
169 BasicBlock& Entry = CS.getCaller()->getEntryBlock(); | |
170 if (Builder.GetInsertBlock() != &Entry) | |
171 Builder.SetInsertPoint(&Entry, Entry.begin()); | |
172 NumGcToStack++; | |
173 } else { | |
174 NumToDynSize++; | |
175 } | |
176 | |
177 // 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
|
178 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
|
179 AllocaInst* alloca = Builder.CreateAlloca(Ty, count, ".nongc_mem"); // FIXME: align? |
1306 | 180 |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
181 if (Initialized) { |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
182 // For now, only zero-init is supported. |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
183 uint64_t size = A.TD.getTypeStoreSize(Ty); |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
184 Value* TypeSize = ConstantInt::get(arrSize->getType(), size); |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
185 // 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
|
186 // allocation site. |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
187 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
|
188 EmitMemZero(B, alloca, Size, A); |
1307
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 |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
191 return alloca; |
1306 | 192 } |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
193 }; |
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
|
194 |
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 // 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
|
196 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
|
197 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
|
198 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
|
199 // 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
|
200 // 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 |
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 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
|
209 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
|
210 |
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 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
|
212 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
|
213 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
|
214 |
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
|
215 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
|
216 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
|
217 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
|
218 |
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 // 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
|
220 // 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
|
221 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
|
222 // 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
|
223 // (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
|
224 // those can be ignored) |
1343
c21a6654cce2
Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents:
1317
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 |
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 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
|
230 != 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
|
231 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
|
232 |
1343
c21a6654cce2
Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents:
1317
diff
changeset
|
233 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
|
234 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
|
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 |
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 // 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
|
238 |
1361
67ac63740c7f
silence a gcc warning
Benjamin Kramer <benny.kra@gmail.com>
parents:
1350
diff
changeset
|
239 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
|
240 }; |
1307
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 //===----------------------------------------------------------------------===// |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
245 // GarbageCollect2Stack Pass Implementation |
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 |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
248 namespace { |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
249 /// This pass replaces GC calls with alloca's |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
250 /// |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
251 class VISIBILITY_HIDDEN GarbageCollect2Stack : public FunctionPass { |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
252 StringMap<FunctionInfo*> KnownFunctions; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
253 Module* M; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
254 |
1306 | 255 FunctionInfo AllocMemoryT; |
256 ArrayFI NewArrayVT; | |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
257 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
|
258 AllocClassFI AllocClass; |
1306 | 259 |
260 public: | |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
261 static char ID; // Pass identification |
1306 | 262 GarbageCollect2Stack(); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
263 |
1306 | 264 bool doInitialization(Module &M) { |
265 this->M = &M; | |
1438
c363d131c1ef
Add some missing returns.
Frits van Bommel <fvbommel wxs.nl>
parents:
1424
diff
changeset
|
266 return false; |
1306 | 267 } |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
268 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
269 bool runOnFunction(Function &F); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
270 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
271 virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
272 AU.addRequired<TargetData>(); |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
273 AU.addRequired<DominatorTree>(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
274 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
275 AU.addPreserved<CallGraph>(); |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
276 AU.addPreserved<DominatorTree>(); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
277 } |
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 char GarbageCollect2Stack::ID = 0; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
280 } // end anonymous namespace. |
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 static RegisterPass<GarbageCollect2Stack> |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
283 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
|
284 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
285 // Public interface to the pass. |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
286 FunctionPass *createGarbageCollect2Stack() { |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
287 return new GarbageCollect2Stack(); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
288 } |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
289 |
1306 | 290 GarbageCollect2Stack::GarbageCollect2Stack() |
291 : FunctionPass(&ID), | |
292 AllocMemoryT(0, true), | |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
293 NewArrayVT(0, true, false, 1), |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
294 NewArrayT(0, true, true, 1) |
1306 | 295 { |
296 KnownFunctions["_d_allocmemoryT"] = &AllocMemoryT; | |
297 KnownFunctions["_d_newarrayvT"] = &NewArrayVT; | |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
298 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
|
299 KnownFunctions["_d_allocclass"] = &AllocClass; |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
300 } |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
301 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
302 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
|
303 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
|
304 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
|
305 // 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
|
306 // 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
|
307 |
7e303f9f16c7
Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents:
1297
diff
changeset
|
308 // 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
|
309 // 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
|
310 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
|
311 // 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
|
312 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
|
313 } |
7e303f9f16c7
Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents:
1297
diff
changeset
|
314 // 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
|
315 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
|
316 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
|
317 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
|
318 } |
7e303f9f16c7
Don't forget to update the control flow when deleting an invoke.
Frits van Bommel <fvbommel wxs.nl>
parents:
1297
diff
changeset
|
319 |
1488
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
320 static bool isSafeToStackAllocate(Instruction* Alloc, DominatorTree& DT); |
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
321 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
322 /// runOnFunction - Top level algorithm. |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
323 /// |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
324 bool GarbageCollect2Stack::runOnFunction(Function &F) { |
1295
0e79fb40c4d0
Remove some overly verbose debug output
Frits van Bommel <fvbommel wxs.nl>
parents:
1291
diff
changeset
|
325 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
|
326 |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
327 TargetData& TD = getAnalysis<TargetData>(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
328 DominatorTree& DT = getAnalysis<DominatorTree>(); |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
329 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
|
330 CallGraphNode* CGNode = CG ? (*CG)[&F] : NULL; |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
331 |
1488
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
332 Analysis A = { TD, *M, CG, CGNode }; |
1306 | 333 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
334 BasicBlock& Entry = F.getEntryBlock(); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
335 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
336 IRBuilder<> AllocaBuilder(&Entry, Entry.begin()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
337 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
338 bool Changed = false; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
339 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
|
340 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
|
341 // Ignore non-calls. |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
342 Instruction* Inst = I++; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
343 CallSite CS = CallSite::get(Inst); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
344 if (!CS.getInstruction()) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
345 continue; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
346 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
347 // 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
|
348 Function *Callee = CS.getCalledFunction(); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
349 if (Callee == 0 || !Callee->isDeclaration() || |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
350 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage())) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
351 continue; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
352 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
353 // Ignore unknown calls. |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
354 const char *CalleeName = Callee->getNameStart(); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
355 StringMap<FunctionInfo*>::iterator OMI = |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
356 KnownFunctions.find(CalleeName, CalleeName+Callee->getNameLen()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
357 if (OMI == KnownFunctions.end()) continue; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
358 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
359 assert(isa<PointerType>(Inst->getType()) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
360 && "GC function doesn't return a pointer?"); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
361 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
362 FunctionInfo* info = OMI->getValue(); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
363 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
364 if (Inst->use_empty() && info->SafeToDelete) { |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
365 Changed = true; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
366 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
|
367 RemoveCall(CS, A); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
368 continue; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
369 } |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
370 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
371 DEBUG(DOUT << "GarbageCollect2Stack inspecting: " << *Inst); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
372 |
1488
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
373 if (!info->analyze(CS, A) || !isSafeToStackAllocate(Inst, DT)) |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
374 continue; |
1305
8215dbf0e09f
Postpone (expensive) escape analysis until we're sure it's needed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1298
diff
changeset
|
375 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
376 // Let's alloca this! |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
377 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
|
378 |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
379 IRBuilder<> Builder(BB, Inst); |
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
380 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
|
381 |
1343
c21a6654cce2
Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents:
1317
diff
changeset
|
382 DEBUG(DOUT << "Promoted to: " << *newVal); |
c21a6654cce2
Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents:
1317
diff
changeset
|
383 |
1297
8e8552601ecd
Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents:
1295
diff
changeset
|
384 // 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
|
385 // 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
|
386 if (newVal->getType() != Inst->getType()) |
1307
e2ec50329af1
Stack-allocate zero-initialized arrays.
Frits van Bommel <fvbommel wxs.nl>
parents:
1306
diff
changeset
|
387 newVal = Builder.CreateBitCast(newVal, Inst->getType()); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
388 Inst->replaceAllUsesWith(newVal); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
389 |
1424
ad999630aa35
Teach -dgc2stack to preserve the call graph. This should allow for more
Frits van Bommel <fvbommel wxs.nl>
parents:
1361
diff
changeset
|
390 RemoveCall(CS, A); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
391 } |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
392 } |
1297
8e8552601ecd
Stack promotion for _d_newarrayvT. Array literals, concatenations (a ~ b) and
Frits van Bommel <fvbommel wxs.nl>
parents:
1295
diff
changeset
|
393 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
394 return Changed; |
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 |
1306 | 397 const Type* Analysis::getTypeFor(Value* typeinfo) const { |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
398 GlobalVariable* ti_global = dyn_cast<GlobalVariable>(typeinfo->stripPointerCasts()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
399 if (!ti_global) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
400 return NULL; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
401 |
1291
875afb7a93b6
Factor out some constants into the header so producers and consumers of
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
402 std::string metaname = TD_PREFIX; |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
403 metaname.append(ti_global->getNameStart(), ti_global->getNameEnd()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
404 |
1306 | 405 GlobalVariable* global = M.getGlobalVariable(metaname); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
406 if (!global || !global->hasInitializer()) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
407 return NULL; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
408 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
409 MDNode* node = dyn_cast<MDNode>(global->getInitializer()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
410 if (!node) |
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 |
1344
3297edb697eb
Re-enable consistency check for fixed LLVM versions.
Frits van Bommel <fvbommel wxs.nl>
parents:
1343
diff
changeset
|
413 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
|
414 return NULL; |
3297edb697eb
Re-enable consistency check for fixed LLVM versions.
Frits van Bommel <fvbommel wxs.nl>
parents:
1343
diff
changeset
|
415 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
|
416 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
|
417 return NULL; |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
418 |
1343
c21a6654cce2
Update for metadata changes in LLVM trunk.
Frits van Bommel <fvbommel wxs.nl>
parents:
1317
diff
changeset
|
419 return MD_GetElement(node, TD_Type)->getType(); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
420 } |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
421 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
422 |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
423 /// isSafeToStackAllocate - Return true if the GC call passed in is safe to turn |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
424 /// into a stack allocation. This requires that the return value does not |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
425 /// escape from the function and no derived pointers are live at the call site |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
426 /// (i.e. if it's in a loop then the function can't use any pointer returned |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
427 /// from an earlier call after a new call has been made) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
428 /// |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
429 /// This is currently conservative where loops are involved: it can handle |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
430 /// simple loops, but returns false if any derived pointer is used in a |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
431 /// subsequent iteration. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
432 /// |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
433 /// Based on LLVM's PointerMayBeCaptured(), which only does escape analysis but |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
434 /// doesn't care about loops. |
1488
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
435 bool isSafeToStackAllocate(Instruction* Alloc, DominatorTree& DT) { |
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
436 assert(isa<PointerType>(Alloc->getType()) && "Allocation is not a pointer?"); |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
437 Value* V = Alloc; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
438 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
439 SmallVector<Use*, 16> Worklist; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
440 SmallSet<Use*, 16> Visited; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
441 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
442 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
443 UI != UE; ++UI) { |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
444 Use *U = &UI.getUse(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
445 Visited.insert(U); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
446 Worklist.push_back(U); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
447 } |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
448 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
449 while (!Worklist.empty()) { |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
450 Use *U = Worklist.pop_back_val(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
451 Instruction *I = cast<Instruction>(U->getUser()); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
452 V = U->get(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
453 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
454 switch (I->getOpcode()) { |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
455 case Instruction::Call: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
456 case Instruction::Invoke: { |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
457 CallSite CS = CallSite::get(I); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
458 // Not captured if the callee is readonly, doesn't return a copy through |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
459 // its return value and doesn't unwind (a readonly function can leak bits |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
460 // by throwing an exception or not depending on the input value). |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
461 if (CS.onlyReadsMemory() && CS.doesNotThrow() && |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
462 I->getType() == Type::VoidTy) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
463 break; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
464 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
465 // Not captured if only passed via 'nocapture' arguments. Note that |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
466 // calling a function pointer does not in itself cause the pointer to |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
467 // be captured. This is a subtle point considering that (for example) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
468 // the callee might return its own address. It is analogous to saying |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
469 // that loading a value from a pointer does not cause the pointer to be |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
470 // captured, even though the loaded value might be the pointer itself |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
471 // (think of self-referential objects). |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
472 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
473 for (CallSite::arg_iterator A = B; A != E; ++A) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
474 if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture)) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
475 // The parameter is not marked 'nocapture' - captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
476 return false; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
477 // Only passed via 'nocapture' arguments, or is the called function - not |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
478 // captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
479 break; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
480 } |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
481 case Instruction::Free: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
482 // Freeing a pointer does not cause it to be captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
483 break; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
484 case Instruction::Load: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
485 // Loading from a pointer does not cause it to be captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
486 break; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
487 case Instruction::Store: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
488 if (V == I->getOperand(0)) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
489 // Stored the pointer - it may be captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
490 return false; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
491 // Storing to the pointee does not cause the pointer to be captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
492 break; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
493 case Instruction::BitCast: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
494 case Instruction::GetElementPtr: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
495 case Instruction::PHI: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
496 case Instruction::Select: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
497 // It's not safe to stack-allocate if this derived pointer is live across |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
498 // the original allocation. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
499 // That can only be true if it dominates the allocation. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
500 // FIXME: To be more precise, it's also only true if it's then used after |
1488
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
501 // the original allocation instruction gets performed again (and |
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
502 // the instruction generating the derived pointer doesn't), but how |
ca31a8e9c42b
Oops, I accidentally pushed r1486 before the last touches were committed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1486
diff
changeset
|
503 // to check that? |
1486
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
504 if (!I->use_empty() && DT.dominates(I, Alloc)) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
505 return false; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
506 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
507 // The original value is not captured via this if the new value isn't. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
508 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
509 UI != UE; ++UI) { |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
510 Use *U = &UI.getUse(); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
511 if (Visited.insert(U)) |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
512 Worklist.push_back(U); |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
513 } |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
514 break; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
515 default: |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
516 // Something else - be conservative and say it is captured. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
517 return false; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
518 } |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
519 } |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
520 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
521 // All uses examined - not captured or live across original allocation. |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
522 return true; |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
523 } |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
524 |
9ed0695cb93c
Teach `-dgc2stack` to promote GC allocations in simple loops to stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1438
diff
changeset
|
525 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
526 #endif //USE_METADATA |