OLD | NEW |
---|---|
(Empty) | |
1 //===- ReplacePtrsWithInts.cpp - Convert pointer values to integer values--===// | |
2 // | |
3 // The LLVM Compiler Infrastructure | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // This pass strips out aggregate pointer types and replaces them with | |
11 // the integer type iPTR, which is i32 for PNaCl (though this pass | |
12 // will allow iPTR to be i64 if the DataLayout specifies 64-bit | |
13 // pointers). | |
14 // | |
15 // The pass converts IR to the following normal form: | |
16 // | |
17 // All inttoptr and ptrtoint instructions use the same integer size | |
18 // (iPTR), so they do not implicitly truncate or zero-extend. | |
19 // | |
20 // alloca always allocates an i8 array type. | |
21 // | |
22 // Pointer types only appear in the following instructions: | |
23 // * loads and stores: the pointer operand is a NormalizedPtr. | |
24 // * function calls: the function operand is a NormalizedPtr. | |
25 // * intrinsic calls: any pointer arguments are NormalizedPtrs. | |
26 // * alloca | |
27 // * bitcast and inttoptr: only used as part of a NormalizedPtr. | |
28 // * ptrtoint: the operand is an InherentPtr. | |
29 // | |
30 // Where an InherentPtr is defined as a pointer value that is: | |
31 // * an alloca; | |
32 // * a GlobalValue (a function or global variable); or | |
33 // * an intrinsic call. | |
34 // | |
35 // And a NormalizedPtr is defined as a pointer value that is: | |
36 // * an inttoptr instruction; | |
37 // * an InherentPtr; or | |
38 // * a bitcast of an InherentPtr. | |
39 // | |
40 //===----------------------------------------------------------------------===// | |
41 | |
42 #include "llvm/ADT/DenseMap.h" | |
43 #include "llvm/IR/DataLayout.h" | |
44 #include "llvm/IR/DerivedTypes.h" | |
45 #include "llvm/IR/Function.h" | |
46 #include "llvm/IR/Instructions.h" | |
47 #include "llvm/IR/IntrinsicInst.h" | |
48 #include "llvm/IR/Module.h" | |
49 #include "llvm/IR/Type.h" | |
50 #include "llvm/Pass.h" | |
51 #include "llvm/Support/raw_ostream.h" | |
52 #include "llvm/Transforms/NaCl.h" | |
53 | |
54 using namespace llvm; | |
55 | |
56 namespace { | |
57 // This is a ModulePass because the pass must recreate functions in | |
58 // order to change their argument and return types. | |
59 struct ReplacePtrsWithInts : public ModulePass { | |
60 static char ID; // Pass identification, replacement for typeid | |
61 ReplacePtrsWithInts() : ModulePass(ID) { | |
62 initializeReplacePtrsWithIntsPass(*PassRegistry::getPassRegistry()); | |
63 } | |
64 | |
65 virtual bool runOnModule(Module &M); | |
66 }; | |
67 | |
68 // FunctionConverter stores the state for mapping old instructions | |
69 // (of pointer type) to converted instructions (of integer type) | |
70 // within a function, and provides methods for doing the conversion. | |
71 class FunctionConverter { | |
72 // Int type that pointer types are to be replaced with, typically i32. | |
73 Type *IntPtrType; | |
74 | |
75 struct RewrittenVal { | |
76 RewrittenVal(): IsPlaceholder(true), NewIntVal(NULL) {} | |
77 bool IsPlaceholder; | |
78 Value *NewIntVal; | |
79 }; | |
80 // Maps from old values (of pointer type) to converted values (of | |
81 // IntPtrType type). | |
82 DenseMap<Value *, RewrittenVal> RewriteMap; | |
83 // Number of placeholders; used for detecting unhandled cases. | |
84 int PlaceholderCount; | |
85 // List of instructions whose deletion has been deferred. | |
86 SmallVector<Instruction *, 20> ToErase; | |
87 | |
88 public: | |
89 FunctionConverter(Type *IntPtrType) | |
90 : IntPtrType(IntPtrType), PlaceholderCount(0) {} | |
91 | |
92 // Returns the normalized version of the given type, converting | |
93 // pointer types to IntPtrType. | |
94 Type *convertType(Type *Ty); | |
95 // Returns the normalized version of the given function type by | |
96 // normalizing the function's argument types. | |
97 FunctionType *convertFuncType(FunctionType *FTy); | |
98 | |
99 // Records that 'To' is the normalized version of 'From'. | |
100 void recordConverted(Value *From, Value *To); | |
101 void recordConvertedAndErase(Instruction *From, Value *To); | |
102 | |
103 // Returns the normalized version of the given value. | |
104 Value *convert(Value *Val); | |
105 // Returns the NormalizedPtr form of the given pointer value. | |
106 Value *convertBackToPtr(Value *Val, Instruction *InsertPt); | |
107 // Returns the NormalizedPtr form of the given function pointer. | |
108 Value *convertFunctionPtr(Value *Callee, Instruction *InsertPt); | |
109 // Converts an instruction without recreating it, by wrapping its | |
110 // operands and result. | |
111 void convertInPlace(Instruction *Inst); | |
112 | |
113 void eraseReplacedInstructions(); | |
114 }; | |
115 } | |
116 | |
117 Type *FunctionConverter::convertType(Type *Ty) { | |
118 if (Ty->isPointerTy()) | |
119 return IntPtrType; | |
120 return Ty; | |
121 } | |
122 | |
123 FunctionType *FunctionConverter::convertFuncType(FunctionType *FTy) { | |
124 SmallVector<Type *, 8> ArgTypes; | |
125 for (FunctionType::param_iterator ArgTy = FTy->param_begin(), | |
126 E = FTy->param_end(); ArgTy != E; ++ArgTy) { | |
127 ArgTypes.push_back(convertType(*ArgTy)); | |
128 } | |
129 return FunctionType::get(convertType(FTy->getReturnType()), ArgTypes, | |
130 FTy->isVarArg()); | |
131 } | |
132 | |
133 void FunctionConverter::recordConverted(Value *From, Value *To) { | |
134 if (!From->getType()->isPointerTy()) { | |
135 From->replaceAllUsesWith(To); | |
136 return; | |
137 } | |
138 RewrittenVal *RV = &RewriteMap[From]; | |
139 if (RV->NewIntVal) { | |
140 assert(RV->IsPlaceholder); | |
141 // Resolve placeholder. | |
142 RV->NewIntVal->replaceAllUsesWith(To); | |
143 delete RV->NewIntVal; | |
144 RV->IsPlaceholder = false; | |
145 --PlaceholderCount; | |
146 } | |
147 RV->NewIntVal = To; | |
148 } | |
149 | |
150 void FunctionConverter::recordConvertedAndErase(Instruction *From, Value *To) { | |
151 recordConverted(From, To); | |
152 // There may still be references to this value, so defer deleting it. | |
153 ToErase.push_back(From); | |
154 } | |
155 | |
156 Value *FunctionConverter::convert(Value *Val) { | |
157 if (!Val->getType()->isPointerTy()) | |
158 return Val; | |
159 if (Constant *C = dyn_cast<Constant>(Val)) | |
160 return ConstantExpr::getPtrToInt(C, IntPtrType); | |
161 RewrittenVal *RV = &RewriteMap[Val]; | |
162 if (!RV->NewIntVal) { | |
163 // No converted value available yet, so create a placeholder. | |
164 Argument *Placeholder = new Argument(convertType(Val->getType())); | |
165 RV->NewIntVal = Placeholder; | |
166 ++PlaceholderCount; | |
167 } | |
168 return RV->NewIntVal; | |
169 } | |
170 | |
171 Value *FunctionConverter::convertBackToPtr(Value *Val, Instruction *InsertPt) { | |
172 Type *NewTy = | |
173 convertType(Val->getType()->getPointerElementType())->getPointerTo(); | |
Derek Schuff
2013/05/17 23:16:05
is this sequence just to convert e.g. from array t
Mark Seaborn
2013/05/20 15:08:12
No, arrays aren't involved. This line converts a
| |
174 Value *Conv = convert(Val); | |
175 return new IntToPtrInst(Conv, NewTy, Conv->getName() + ".asptr", InsertPt); | |
176 } | |
177 | |
178 Value *FunctionConverter::convertFunctionPtr(Value *Callee, | |
179 Instruction *InsertPt) { | |
180 FunctionType *FuncType = cast<FunctionType>( | |
181 Callee->getType()->getPointerElementType()); | |
182 Value *Conv = convert(Callee); | |
183 return new IntToPtrInst(Conv, convertFuncType(FuncType)->getPointerTo(), | |
184 Conv->getName() + ".asfuncptr", InsertPt); | |
185 } | |
186 | |
187 static bool ShouldLeaveAlone(Value *V) { | |
188 if (Function *F = dyn_cast<Function>(V)) | |
189 return F->isIntrinsic(); | |
190 if (isa<InlineAsm>(V)) | |
191 return true; | |
192 return false; | |
193 } | |
194 | |
195 void FunctionConverter::convertInPlace(Instruction *Inst) { | |
196 // Convert operands. | |
197 for (unsigned I = 0; I < Inst->getNumOperands(); ++I) { | |
198 Value *Arg = Inst->getOperand(I); | |
199 if (Arg->getType()->isPointerTy() && !ShouldLeaveAlone(Arg)) { | |
200 Value *Conv = convert(Arg); | |
201 Inst->setOperand(I, new IntToPtrInst(convert(Arg), Arg->getType(), | |
202 Conv->getName() + ".asptr", Inst)); | |
203 } | |
204 } | |
205 // Convert result. | |
206 if (Inst->getType()->isPointerTy()) { | |
207 Instruction *Cast = new PtrToIntInst( | |
208 Inst, convertType(Inst->getType()), Inst->getName() + ".asint"); | |
209 Cast->insertAfter(Inst); | |
210 recordConverted(Inst, Cast); | |
211 } | |
212 } | |
213 | |
214 void FunctionConverter::eraseReplacedInstructions() { | |
215 if (PlaceholderCount) { | |
216 for (DenseMap<Value *, RewrittenVal>::iterator I = RewriteMap.begin(), | |
217 E = RewriteMap.end(); I != E; ++I) { | |
218 if (I->second.IsPlaceholder) | |
219 errs() << "Not converted: " << *I->first << "\n"; | |
220 } | |
221 report_fatal_error("Case not handled in ReplacePtrsWithInts"); | |
222 } | |
223 for (SmallVector<Instruction *, 10>::iterator I = ToErase.begin(), | |
Derek Schuff
2013/05/17 23:16:05
the standard idiom in LLVM in this kind of situati
Mark Seaborn
2013/05/20 15:08:12
Done.
| |
224 E = ToErase.end(); | |
225 I != E; ++I) { | |
226 (*I)->dropAllReferences(); | |
227 } | |
228 for (SmallVector<Instruction *, 10>::iterator I = ToErase.begin(), | |
229 E = ToErase.end(); | |
230 I != E; ++I) { | |
231 (*I)->eraseFromParent(); | |
232 } | |
233 } | |
234 | |
235 static void ConvertMetadataOperand(FunctionConverter *FC, | |
236 IntrinsicInst *Call, int Index) { | |
237 MDNode *MD = cast<MDNode>(Call->getArgOperand(Index)); | |
238 if (MD->getNumOperands() != 1) | |
239 return; | |
240 Value *MDArg = MD->getOperand(0); | |
241 if (MDArg && (isa<Argument>(MDArg) || isa<Instruction>(MDArg))) { | |
242 MDArg = FC->convert(MDArg); | |
243 if (PtrToIntInst *Cast = dyn_cast<PtrToIntInst>(MDArg)) { | |
244 // Unwrapping this is necessary for llvm.dbg.declare to work. | |
245 MDArg = Cast->getPointerOperand(); | |
246 } | |
247 SmallVector<Value *, 1> Args; | |
248 Args.push_back(MDArg); | |
249 Call->setArgOperand(Index, MDNode::get(Call->getContext(), Args)); | |
250 } | |
251 } | |
252 | |
253 static AttributeSet RemoveAttrs(LLVMContext &Context, AttributeSet Attrs) { | |
254 SmallVector<AttributeSet, 8> AttrList; | |
255 for (unsigned Slot = 0; Slot < Attrs.getNumSlots(); ++Slot) { | |
256 unsigned Index = Attrs.getSlotIndex(Slot); | |
257 AttrBuilder AB; | |
258 for (AttributeSet::iterator Attr = Attrs.begin(Slot), E = Attrs.end(Slot); | |
259 Attr != E; ++Attr) { | |
260 switch (Attr->getKindAsEnum()) { | |
261 case Attribute::ByVal: | |
262 case Attribute::StructRet: | |
263 case Attribute::Nest: | |
264 Attrs.dump(); | |
265 report_fatal_error("ReplacePtrsWithInts cannot handle " | |
266 "byval, sret or nest attrs"); | |
267 break; | |
268 case Attribute::NoCapture: | |
269 case Attribute::NoAlias: | |
270 // Strip these attributes. | |
271 break; | |
272 default: | |
273 AB.addAttribute(*Attr); | |
274 } | |
275 } | |
276 AttrList.push_back(AttributeSet::get(Context, Index, AB)); | |
277 } | |
278 return AttributeSet::get(Context, AttrList); | |
279 } | |
280 | |
281 template <class InstType> | |
282 static void CopyLoadOrStoreAttrs(InstType *Dest, InstType *Src) { | |
283 Dest->setVolatile(Src->isVolatile()); | |
284 Dest->setAlignment(Src->getAlignment()); | |
285 Dest->setOrdering(Src->getOrdering()); | |
286 Dest->setSynchScope(Src->getSynchScope()); | |
287 } | |
288 | |
289 static void ConvertInstruction(DataLayout *DL, Type *IntPtrType, | |
290 FunctionConverter *FC, Instruction *Inst) { | |
291 if (ReturnInst *Ret = dyn_cast<ReturnInst>(Inst)) { | |
292 Value *Result = Ret->getReturnValue(); | |
293 if (Result) | |
294 Result = FC->convert(Result); | |
295 CopyDebug(ReturnInst::Create(Ret->getContext(), Result, Ret), Inst); | |
296 Ret->eraseFromParent(); | |
297 } else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) { | |
298 PHINode *Phi2 = PHINode::Create(FC->convertType(Phi->getType()), | |
299 Phi->getNumIncomingValues(), | |
300 "", Phi); | |
301 CopyDebug(Phi2, Phi); | |
302 for (unsigned I = 0; I < Phi->getNumIncomingValues(); ++I) { | |
303 Phi2->addIncoming(FC->convert(Phi->getIncomingValue(I)), | |
304 Phi->getIncomingBlock(I)); | |
305 } | |
306 Phi2->takeName(Phi); | |
307 FC->recordConvertedAndErase(Phi, Phi2); | |
308 } else if (SelectInst *Op = dyn_cast<SelectInst>(Inst)) { | |
309 Instruction *Op2 = SelectInst::Create(Op->getCondition(), | |
310 FC->convert(Op->getTrueValue()), | |
311 FC->convert(Op->getFalseValue()), | |
312 "", Op); | |
313 CopyDebug(Op2, Op); | |
314 Op2->takeName(Op); | |
315 FC->recordConvertedAndErase(Op, Op2); | |
316 } else if (isa<PtrToIntInst>(Inst) || isa<IntToPtrInst>(Inst)) { | |
317 Value *Arg = FC->convert(Inst->getOperand(0)); | |
318 unsigned ArgSize = Arg->getType()->getIntegerBitWidth(); | |
319 Type *ResultTy = FC->convertType(Inst->getType()); | |
320 unsigned ResultSize = ResultTy->getIntegerBitWidth(); | |
321 Value *Result; | |
322 if (ArgSize == ResultSize) { | |
Derek Schuff
2013/05/17 23:16:05
You could probably just make an IRBuilder and use
Mark Seaborn
2013/05/20 15:08:12
Done.
| |
323 Result = Arg; | |
324 } else { | |
325 if (ArgSize > ResultSize) { | |
326 Result = CopyDebug(new TruncInst(Arg, ResultTy, "", Inst), Inst); | |
327 } else { | |
328 Result = CopyDebug(new ZExtInst(Arg, ResultTy, "", Inst), Inst); | |
329 } | |
330 Result->takeName(Inst); | |
331 } | |
332 FC->recordConvertedAndErase(Inst, Result); | |
333 } else if (isa<BitCastInst>(Inst)) { | |
334 if (Inst->getType()->isPointerTy()) { | |
335 FC->recordConvertedAndErase(Inst, FC->convert(Inst->getOperand(0))); | |
336 } | |
337 } else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Inst)) { | |
338 Value *Cmp2 = CopyDebug(new ICmpInst(Inst, Cmp->getPredicate(), | |
339 FC->convert(Cmp->getOperand(0)), | |
340 FC->convert(Cmp->getOperand(1)), ""), | |
341 Inst); | |
342 Cmp2->takeName(Cmp); | |
343 Cmp->replaceAllUsesWith(Cmp2); | |
344 Cmp->eraseFromParent(); | |
345 } else if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { | |
346 Value *Ptr = FC->convertBackToPtr(Load->getPointerOperand(), Inst); | |
347 LoadInst *Result = new LoadInst(Ptr, "", Inst); | |
348 Result->takeName(Inst); | |
349 CopyDebug(Result, Inst); | |
350 CopyLoadOrStoreAttrs(Result, Load); | |
351 FC->recordConvertedAndErase(Inst, Result); | |
352 } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) { | |
353 Value *Ptr = FC->convertBackToPtr(Store->getPointerOperand(), Inst); | |
354 StoreInst *Result = new StoreInst(FC->convert(Store->getValueOperand()), | |
355 Ptr, Inst); | |
356 CopyDebug(Result, Inst); | |
357 CopyLoadOrStoreAttrs(Result, Store); | |
358 Inst->eraseFromParent(); | |
359 } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { | |
360 if (IntrinsicInst *ICall = dyn_cast<IntrinsicInst>(Inst)) { | |
361 if (ICall->getIntrinsicID() == Intrinsic::dbg_declare) { | |
362 ConvertMetadataOperand(FC, ICall, 0); | |
363 } | |
364 FC->convertInPlace(Inst); | |
365 } else if (isa<InlineAsm>(Call->getCalledValue())) { | |
366 FC->convertInPlace(Inst); | |
367 } else { | |
368 SmallVector<Value *, 10> Args; | |
369 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) | |
370 Args.push_back(FC->convert(Call->getArgOperand(I))); | |
371 CallInst *NewCall = CallInst::Create( | |
372 FC->convertFunctionPtr(Call->getCalledValue(), Call), | |
373 Args, "", Inst); | |
374 CopyDebug(NewCall, Call); | |
375 NewCall->setAttributes(RemoveAttrs(Call->getContext(), | |
376 Call->getAttributes())); | |
377 NewCall->setCallingConv(Call->getCallingConv()); | |
378 NewCall->takeName(Call); | |
379 FC->recordConvertedAndErase(Call, NewCall); | |
380 } | |
381 } else if (InvokeInst *Call = dyn_cast<InvokeInst>(Inst)) { | |
382 SmallVector<Value *, 10> Args; | |
383 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) | |
384 Args.push_back(FC->convert(Call->getArgOperand(I))); | |
385 InvokeInst *NewCall = InvokeInst::Create( | |
386 FC->convertFunctionPtr(Call->getCalledValue(), Call), | |
387 Call->getNormalDest(), | |
388 Call->getUnwindDest(), | |
389 Args, "", Inst); | |
390 CopyDebug(NewCall, Call); | |
391 NewCall->setAttributes(RemoveAttrs(Call->getContext(), | |
392 Call->getAttributes())); | |
393 NewCall->setCallingConv(Call->getCallingConv()); | |
394 NewCall->takeName(Call); | |
395 FC->recordConvertedAndErase(Call, NewCall); | |
396 } else if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Inst)) { | |
397 Type *ElementTy = Inst->getType()->getPointerElementType(); | |
398 Type *ElementTy2 = ArrayType::get(Type::getInt8Ty(Inst->getContext()), | |
399 DL->getTypeAllocSize(ElementTy)); | |
400 Value *Tmp = CopyDebug(new AllocaInst(ElementTy2, Alloca->getArraySize(), | |
401 Alloca->getAlignment(), "", Inst), | |
402 Inst); | |
403 Tmp->takeName(Alloca); | |
404 Value *Alloca2 = new PtrToIntInst(Tmp, IntPtrType, | |
405 Tmp->getName() + ".asint", Inst); | |
406 FC->recordConvertedAndErase(Alloca, Alloca2); | |
407 } else if (// These atomics only operate on integer pointers, not | |
408 // other pointers, so we don't need to recreate the | |
409 // instruction. | |
410 isa<AtomicCmpXchgInst>(Inst) || | |
411 isa<AtomicRMWInst>(Inst) || | |
412 // Handle these instructions as a convenience to allow | |
413 // the pass to be used in more situations, even though we | |
414 // don't expect them in PNaCl's stable ABI. | |
415 isa<GetElementPtrInst>(Inst) || | |
416 isa<VAArgInst>(Inst) || | |
417 isa<IndirectBrInst>(Inst) || | |
418 isa<ExtractValueInst>(Inst) || | |
419 isa<InsertValueInst>(Inst)) { | |
420 FC->convertInPlace(Inst); | |
421 } | |
422 } | |
423 | |
424 // Convert ptrtoint+inttoptr to a bitcast because it's shorter and | |
425 // because some intrinsics work on bitcasts but not on | |
426 // ptrtoint+inttoptr, in particular: | |
427 // * llvm.lifetime.start/end | |
428 // * llvm.eh.typeid.for | |
429 static void SimplifyCasts(Instruction *Inst, Type *IntPtrType) { | |
430 if (IntToPtrInst *Cast1 = dyn_cast<IntToPtrInst>(Inst)) { | |
431 if (PtrToIntInst *Cast2 = dyn_cast<PtrToIntInst>(Cast1->getOperand(0))) { | |
432 assert(Cast2->getType() == IntPtrType); | |
433 Value *V = Cast2->getPointerOperand(); | |
434 if (V->getType() != Cast1->getType()) | |
435 V = new BitCastInst(V, Cast1->getType(), V->getName() + ".bc", Cast1); | |
436 Cast1->replaceAllUsesWith(V); | |
437 if (Cast1->use_empty()) | |
438 Cast1->eraseFromParent(); | |
439 if (Cast2->use_empty()) | |
440 Cast2->eraseFromParent(); | |
441 } | |
442 } | |
443 } | |
444 | |
445 static void CleanUpFunction(Function *Func, Type *IntPtrType) { | |
446 // Remove the ptrtoint/bitcast ConstantExprs we introduced for | |
447 // referencing globals. | |
448 FunctionPass *Pass = createExpandConstantExprPass(); | |
449 Pass->runOnFunction(*Func); | |
450 delete Pass; | |
451 | |
452 for (Function::iterator BB = Func->begin(), E = Func->end(); | |
453 BB != E; ++BB) { | |
454 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
455 Iter != E; ) { | |
456 SimplifyCasts(Iter++, IntPtrType); | |
457 } | |
458 } | |
459 // Cleanup: Remove ptrtoints that were introduced for allocas but not used. | |
460 for (Function::iterator BB = Func->begin(), E = Func->end(); | |
461 BB != E; ++BB) { | |
462 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
463 Iter != E; ) { | |
464 Instruction *Inst = Iter++; | |
465 if (isa<PtrToIntInst>(Inst) && Inst->use_empty()) | |
466 Inst->eraseFromParent(); | |
467 } | |
468 } | |
469 } | |
470 | |
471 char ReplacePtrsWithInts::ID = 0; | |
472 INITIALIZE_PASS(ReplacePtrsWithInts, "replace-ptrs-with-ints", | |
473 "Convert pointer values to integer values", | |
474 false, false) | |
475 | |
476 bool ReplacePtrsWithInts::runOnModule(Module &M) { | |
477 DataLayout DL(&M); | |
478 Type *IntPtrType = DL.getIntPtrType(M.getContext()); | |
479 | |
480 for (Module::iterator Iter = M.begin(), E = M.end(); Iter != E; ) { | |
481 Function *OldFunc = Iter++; | |
482 // Intrinsics' types must be left alone. | |
483 if (OldFunc->isIntrinsic()) | |
484 continue; | |
485 | |
486 FunctionConverter FC(IntPtrType); | |
487 FunctionType *NFTy = FC.convertFuncType(OldFunc->getFunctionType()); | |
488 | |
489 // In order to change the function's argument types, we have to | |
490 // recreate the function. | |
491 Function *NewFunc = Function::Create(NFTy, OldFunc->getLinkage()); | |
492 NewFunc->copyAttributesFrom(OldFunc); | |
493 NewFunc->setAttributes(RemoveAttrs(M.getContext(), | |
494 NewFunc->getAttributes())); | |
495 OldFunc->getParent()->getFunctionList().insert(OldFunc, NewFunc); | |
496 NewFunc->takeName(OldFunc); | |
497 NewFunc->getBasicBlockList().splice(NewFunc->begin(), | |
498 OldFunc->getBasicBlockList()); | |
499 | |
500 // Move the arguments across to the new function. | |
501 for (Function::arg_iterator Arg = OldFunc->arg_begin(), | |
502 E = OldFunc->arg_end(), NewArg = NewFunc->arg_begin(); | |
503 Arg != E; ++Arg, ++NewArg) { | |
504 FC.recordConverted(Arg, NewArg); | |
505 NewArg->takeName(Arg); | |
506 } | |
507 | |
508 // Convert the function body. | |
509 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); | |
510 BB != E; ++BB) { | |
511 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
512 Iter != E; ) { | |
513 ConvertInstruction(&DL, IntPtrType, &FC, Iter++); | |
514 } | |
515 } | |
516 FC.eraseReplacedInstructions(); | |
517 OldFunc->replaceAllUsesWith(ConstantExpr::getBitCast(NewFunc, | |
518 OldFunc->getType())); | |
519 OldFunc->eraseFromParent(); | |
520 } | |
521 // Now that all functions have their normalized types, we can remove | |
522 // various casts. | |
523 for (Module::iterator Func = M.begin(), E = M.end(); Func != E; ++Func) { | |
524 CleanUpFunction(Func, IntPtrType); | |
525 } | |
526 return true; | |
527 } | |
528 | |
529 ModulePass *llvm::createReplacePtrsWithIntsPass() { | |
530 return new ReplacePtrsWithInts(); | |
531 } | |
OLD | NEW |