Chromium Code Reviews| 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 typedef DenseMap<Function *, Function *> FunctionMap; | |
| 69 | |
| 70 class FunctionConverter { | |
|
eliben
2013/05/15 22:11:21
Comment explaining what this class does, and what
Mark Seaborn
2013/05/16 02:08:40
Comments added.
| |
| 71 Type *IntPtrType; | |
| 72 FunctionMap *FuncMap; | |
| 73 | |
| 74 struct RewrittenVal { | |
| 75 RewrittenVal(): IsPlaceholder(true), NewIntVal(NULL) {} | |
| 76 bool IsPlaceholder; | |
| 77 Value *NewIntVal; | |
| 78 }; | |
| 79 DenseMap<Value *, RewrittenVal> RewriteMap; | |
| 80 int PlaceholderCount; | |
| 81 SmallVector<Instruction *, 20> ToErase; | |
| 82 | |
| 83 public: | |
| 84 FunctionConverter(Type *IntPtrType, FunctionMap *FuncMap) | |
| 85 : IntPtrType(IntPtrType), FuncMap(FuncMap), PlaceholderCount(0) {} | |
| 86 | |
| 87 Type *ConvertType(Type *Ty); | |
|
eliben
2013/05/15 22:11:21
According to the LLVM convention (http://llvm.org/
Mark Seaborn
2013/05/16 02:08:40
Oops, yes. Fixed the methods.
| |
| 88 FunctionType *ConvertFuncType(FunctionType *FTy); | |
| 89 | |
| 90 void RecordConverted(Value *From, Value *To); | |
| 91 void RecordConvertedAndErase(Instruction *From, Value *To); | |
| 92 Value *Convert(Value *Val); | |
| 93 Value *ConvertBackToPtr(Value *Val, Instruction *InsertPt); | |
| 94 Value *ConvertFunctionPtr(Value *Callee, Instruction *InsertPt); | |
| 95 void ConvertInPlace(Instruction *Inst); | |
| 96 void Finish(); | |
| 97 }; | |
| 98 } | |
| 99 | |
| 100 static Instruction *CopyDebug(Instruction *NewInst, Instruction *Original) { | |
|
eliben
2013/05/15 22:11:21
Hmm, this is being used in ExpandVarArgs too, so m
Mark Seaborn
2013/05/16 02:08:40
OK, done.
| |
| 101 NewInst->setDebugLoc(Original->getDebugLoc()); | |
| 102 return NewInst; | |
| 103 } | |
| 104 | |
| 105 Type *FunctionConverter::ConvertType(Type *Ty) { | |
| 106 if (Ty->isPointerTy()) | |
| 107 return IntPtrType; | |
| 108 return Ty; | |
| 109 } | |
| 110 | |
| 111 FunctionType *FunctionConverter::ConvertFuncType(FunctionType *FTy) { | |
| 112 SmallVector<Type *, 8> ArgTypes; | |
| 113 for (FunctionType::param_iterator ArgTy = FTy->param_begin(), | |
| 114 E = FTy->param_end(); ArgTy != E; ++ArgTy) { | |
| 115 ArgTypes.push_back(ConvertType(*ArgTy)); | |
| 116 } | |
| 117 return FunctionType::get(ConvertType(FTy->getReturnType()), ArgTypes, | |
| 118 FTy->isVarArg()); | |
| 119 } | |
| 120 | |
| 121 void FunctionConverter::RecordConverted(Value *From, Value *To) { | |
| 122 if (!From->getType()->isPointerTy()) { | |
| 123 From->replaceAllUsesWith(To); | |
| 124 return; | |
| 125 } | |
| 126 RewrittenVal *RV = &RewriteMap[From]; | |
| 127 if (RV->NewIntVal) { | |
| 128 assert(RV->IsPlaceholder); | |
| 129 // Resolve placeholder. | |
| 130 RV->NewIntVal->replaceAllUsesWith(To); | |
| 131 delete RV->NewIntVal; | |
| 132 RV->IsPlaceholder = false; | |
| 133 --PlaceholderCount; | |
| 134 } | |
| 135 RV->NewIntVal = To; | |
| 136 } | |
| 137 | |
| 138 void FunctionConverter::RecordConvertedAndErase(Instruction *From, Value *To) { | |
| 139 RecordConverted(From, To); | |
| 140 // There may still be references to this value, so defer deleting it. | |
| 141 ToErase.push_back(From); | |
| 142 } | |
| 143 | |
| 144 Value *FunctionConverter::Convert(Value *Val) { | |
| 145 if (!Val->getType()->isPointerTy()) | |
| 146 return Val; | |
| 147 if (Constant *C = dyn_cast<Constant>(Val)) { | |
| 148 if (Function *Func = dyn_cast<Function>(Val)) { | |
| 149 assert(FuncMap->count(Func) == 1); | |
| 150 C = (*FuncMap)[Func]; | |
| 151 } | |
| 152 return ConstantExpr::getPtrToInt(C, IntPtrType); | |
| 153 } | |
| 154 RewrittenVal *RV = &RewriteMap[Val]; | |
| 155 if (!RV->NewIntVal) { | |
| 156 // No converted value available yet, so create a placeholder. | |
| 157 Argument *Placeholder = new Argument(ConvertType(Val->getType())); | |
| 158 RV->NewIntVal = Placeholder; | |
| 159 ++PlaceholderCount; | |
| 160 } | |
| 161 return RV->NewIntVal; | |
| 162 } | |
| 163 | |
| 164 Value *FunctionConverter::ConvertBackToPtr(Value *Val, Instruction *InsertPt) { | |
| 165 Type *NewTy = | |
| 166 ConvertType(Val->getType()->getPointerElementType())->getPointerTo(); | |
| 167 Value *Conv = Convert(Val); | |
| 168 return new IntToPtrInst(Conv, NewTy, Conv->getName() + ".asptr", InsertPt); | |
| 169 } | |
| 170 | |
| 171 Value *FunctionConverter::ConvertFunctionPtr(Value *Callee, | |
| 172 Instruction *InsertPt) { | |
| 173 // Avoid casts for direct calls. | |
| 174 if (Function *CalleeF = dyn_cast<Function>(Callee)) { | |
| 175 assert(FuncMap->count(CalleeF) == 1); | |
| 176 return (*FuncMap)[CalleeF]; | |
| 177 } | |
| 178 FunctionType *FuncType = cast<FunctionType>( | |
| 179 Callee->getType()->getPointerElementType()); | |
| 180 Value *Conv = Convert(Callee); | |
| 181 return new IntToPtrInst(Conv, ConvertFuncType(FuncType)->getPointerTo(), | |
| 182 Conv->getName() + ".asfuncptr", InsertPt); | |
| 183 } | |
| 184 | |
| 185 static bool ShouldLeaveAlone(Value *V) { | |
| 186 if (Function *F = dyn_cast<Function>(V)) | |
| 187 return F->isIntrinsic(); | |
| 188 if (isa<InlineAsm>(V)) | |
| 189 return true; | |
| 190 return false; | |
| 191 } | |
| 192 | |
| 193 void FunctionConverter::ConvertInPlace(Instruction *Inst) { | |
| 194 // Convert operands. | |
| 195 for (unsigned I = 0; I < Inst->getNumOperands(); ++I) { | |
| 196 Value *Arg = Inst->getOperand(I); | |
| 197 if (Arg->getType()->isPointerTy() && !ShouldLeaveAlone(Arg)) { | |
| 198 Value *Conv = Convert(Arg); | |
| 199 Inst->setOperand(I, new IntToPtrInst(Convert(Arg), Arg->getType(), | |
| 200 Conv->getName() + ".asptr", Inst)); | |
| 201 } | |
| 202 } | |
| 203 // Convert result. | |
| 204 if (Inst->getType()->isPointerTy()) { | |
| 205 Instruction *Cast = new PtrToIntInst( | |
| 206 Inst, ConvertType(Inst->getType()), Inst->getName() + ".asint"); | |
| 207 Cast->insertAfter(Inst); | |
| 208 RecordConverted(Inst, Cast); | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 void FunctionConverter::Finish() { | |
| 213 if (PlaceholderCount) { | |
| 214 for (DenseMap<Value *, RewrittenVal>::iterator I = RewriteMap.begin(), | |
| 215 E = RewriteMap.end(); I != E; ++I) { | |
| 216 if (I->second.IsPlaceholder) | |
| 217 errs() << "Not converted: " << *I->first << "\n"; | |
| 218 } | |
| 219 report_fatal_error("Case not handled in ReplacePtrsWithInts"); | |
| 220 } | |
| 221 for (SmallVector<Instruction *, 10>::iterator I = ToErase.begin(), | |
| 222 E = ToErase.end(); | |
| 223 I != E; ++I) { | |
| 224 (*I)->dropAllReferences(); | |
| 225 } | |
| 226 for (SmallVector<Instruction *, 10>::iterator I = ToErase.begin(), | |
| 227 E = ToErase.end(); | |
| 228 I != E; ++I) { | |
| 229 (*I)->eraseFromParent(); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 static void ConvertMetadataOperand(FunctionConverter *FC, | |
| 234 IntrinsicInst *Call, int Index) { | |
| 235 MDNode *MD = cast<MDNode>(Call->getArgOperand(Index)); | |
| 236 if (MD->getNumOperands() != 1) | |
| 237 return; | |
| 238 Value *MDArg = MD->getOperand(0); | |
| 239 if (MDArg && (isa<Argument>(MDArg) || isa<Instruction>(MDArg))) { | |
| 240 MDArg = FC->Convert(MDArg); | |
| 241 if (PtrToIntInst *Cast = dyn_cast<PtrToIntInst>(MDArg)) { | |
| 242 // Unwrapping this is necessary for llvm.dbg.declare to work. | |
| 243 MDArg = Cast->getPointerOperand(); | |
| 244 } | |
| 245 SmallVector<Value *, 1> Args; | |
| 246 Args.push_back(MDArg); | |
| 247 Call->setArgOperand(Index, MDNode::get(Call->getContext(), Args)); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 static AttributeSet RemoveAttrs(LLVMContext &Context, AttributeSet Attrs) { | |
| 252 SmallVector<AttributeSet, 8> AttrList; | |
| 253 for (unsigned Slot = 0; Slot < Attrs.getNumSlots(); ++Slot) { | |
| 254 unsigned Index = Attrs.getSlotIndex(Slot); | |
| 255 AttrBuilder AB; | |
| 256 for (AttributeSet::iterator Attr = Attrs.begin(Slot), E = Attrs.end(Slot); | |
| 257 Attr != E; ++Attr) { | |
| 258 switch (Attr->getKindAsEnum()) { | |
| 259 case Attribute::ByVal: | |
| 260 case Attribute::StructRet: | |
| 261 case Attribute::Nest: | |
| 262 Attrs.dump(); | |
| 263 report_fatal_error("ReplacePtrsWithInts cannot handle " | |
| 264 "byval, sret or nest attrs"); | |
| 265 break; | |
| 266 case Attribute::NoCapture: | |
| 267 case Attribute::NoAlias: | |
| 268 // Strip these attributes. | |
| 269 break; | |
| 270 default: | |
| 271 AB.addAttribute(*Attr); | |
| 272 } | |
| 273 } | |
| 274 AttrList.push_back(AttributeSet::get(Context, Index, AB)); | |
| 275 } | |
| 276 return AttributeSet::get(Context, AttrList); | |
| 277 } | |
| 278 | |
| 279 template <class InstType> | |
| 280 static void CopyLoadOrStoreAttrs(InstType *Dest, InstType *Src) { | |
| 281 Dest->setVolatile(Src->isVolatile()); | |
| 282 Dest->setAlignment(Src->getAlignment()); | |
| 283 Dest->setOrdering(Src->getOrdering()); | |
| 284 Dest->setSynchScope(Src->getSynchScope()); | |
| 285 } | |
| 286 | |
| 287 static void ConvertInstruction(DataLayout *DL, Type *IntPtrType, | |
| 288 FunctionConverter *FC, Instruction *Inst) { | |
| 289 if (ReturnInst *Ret = dyn_cast<ReturnInst>(Inst)) { | |
| 290 Value *Result = Ret->getReturnValue(); | |
| 291 if (Result) | |
| 292 Result = FC->Convert(Result); | |
| 293 CopyDebug(ReturnInst::Create(Ret->getContext(), Result, Ret), Inst); | |
| 294 Ret->eraseFromParent(); | |
| 295 } else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) { | |
| 296 PHINode *Phi2 = PHINode::Create(FC->ConvertType(Phi->getType()), | |
| 297 Phi->getNumIncomingValues(), | |
| 298 "", Phi); | |
| 299 CopyDebug(Phi2, Phi); | |
| 300 for (unsigned I = 0; I < Phi->getNumIncomingValues(); ++I) { | |
| 301 Phi2->addIncoming(FC->Convert(Phi->getIncomingValue(I)), | |
| 302 Phi->getIncomingBlock(I)); | |
| 303 } | |
| 304 Phi2->takeName(Phi); | |
| 305 FC->RecordConvertedAndErase(Phi, Phi2); | |
| 306 } else if (SelectInst *Op = dyn_cast<SelectInst>(Inst)) { | |
| 307 Instruction *Op2 = SelectInst::Create(Op->getCondition(), | |
| 308 FC->Convert(Op->getTrueValue()), | |
| 309 FC->Convert(Op->getFalseValue()), | |
| 310 "", Op); | |
| 311 CopyDebug(Op2, Op); | |
| 312 Op2->takeName(Op); | |
| 313 FC->RecordConvertedAndErase(Op, Op2); | |
| 314 } else if (isa<PtrToIntInst>(Inst) || isa<IntToPtrInst>(Inst)) { | |
| 315 Value *Arg = FC->Convert(Inst->getOperand(0)); | |
| 316 unsigned ArgSize = Arg->getType()->getIntegerBitWidth(); | |
| 317 Type *ResultTy = FC->ConvertType(Inst->getType()); | |
| 318 unsigned ResultSize = ResultTy->getIntegerBitWidth(); | |
| 319 Value *Result; | |
| 320 if (ArgSize == ResultSize) { | |
| 321 Result = Arg; | |
| 322 } else { | |
| 323 if (ArgSize > ResultSize) { | |
| 324 Result = CopyDebug(new TruncInst(Arg, ResultTy, "", Inst), Inst); | |
| 325 } else { | |
| 326 Result = CopyDebug(new ZExtInst(Arg, ResultTy, "", Inst), Inst); | |
| 327 } | |
| 328 Result->takeName(Inst); | |
| 329 } | |
| 330 FC->RecordConvertedAndErase(Inst, Result); | |
| 331 } else if (isa<BitCastInst>(Inst)) { | |
| 332 if (Inst->getType()->isPointerTy()) { | |
| 333 FC->RecordConvertedAndErase(Inst, FC->Convert(Inst->getOperand(0))); | |
| 334 } | |
| 335 } else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Inst)) { | |
| 336 Value *Cmp2 = CopyDebug(new ICmpInst(Inst, Cmp->getPredicate(), | |
| 337 FC->Convert(Cmp->getOperand(0)), | |
| 338 FC->Convert(Cmp->getOperand(1)), ""), | |
| 339 Inst); | |
| 340 Cmp2->takeName(Cmp); | |
| 341 Cmp->replaceAllUsesWith(Cmp2); | |
| 342 Cmp->eraseFromParent(); | |
| 343 } else if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { | |
| 344 Value *Ptr = FC->ConvertBackToPtr(Load->getPointerOperand(), Inst); | |
| 345 LoadInst *Result = new LoadInst(Ptr, "", Inst); | |
| 346 Result->takeName(Inst); | |
| 347 CopyDebug(Result, Inst); | |
| 348 CopyLoadOrStoreAttrs(Result, Load); | |
| 349 FC->RecordConvertedAndErase(Inst, Result); | |
| 350 } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) { | |
| 351 Value *Ptr = FC->ConvertBackToPtr(Store->getPointerOperand(), Inst); | |
| 352 StoreInst *Result = new StoreInst(FC->Convert(Store->getValueOperand()), | |
| 353 Ptr, Inst); | |
| 354 CopyDebug(Result, Inst); | |
| 355 CopyLoadOrStoreAttrs(Result, Store); | |
| 356 Inst->eraseFromParent(); | |
| 357 } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { | |
| 358 if (IntrinsicInst *ICall = dyn_cast<IntrinsicInst>(Inst)) { | |
| 359 if (ICall->getIntrinsicID() == Intrinsic::dbg_declare) { | |
| 360 ConvertMetadataOperand(FC, ICall, 0); | |
| 361 } | |
| 362 FC->ConvertInPlace(Inst); | |
| 363 } else if (isa<InlineAsm>(Call->getCalledValue())) { | |
| 364 FC->ConvertInPlace(Inst); | |
| 365 } else { | |
| 366 SmallVector<Value *, 10> Args; | |
| 367 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) | |
| 368 Args.push_back(FC->Convert(Call->getArgOperand(I))); | |
| 369 CallInst *NewCall = CallInst::Create( | |
| 370 FC->ConvertFunctionPtr(Call->getCalledValue(), Call), | |
| 371 Args, "", Inst); | |
| 372 CopyDebug(NewCall, Call); | |
| 373 NewCall->setAttributes(RemoveAttrs(Call->getContext(), | |
| 374 Call->getAttributes())); | |
| 375 NewCall->setCallingConv(Call->getCallingConv()); | |
| 376 NewCall->takeName(Call); | |
| 377 FC->RecordConvertedAndErase(Call, NewCall); | |
| 378 } | |
| 379 } else if (InvokeInst *Call = dyn_cast<InvokeInst>(Inst)) { | |
| 380 SmallVector<Value *, 10> Args; | |
| 381 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) | |
| 382 Args.push_back(FC->Convert(Call->getArgOperand(I))); | |
| 383 InvokeInst *NewCall = InvokeInst::Create( | |
| 384 FC->ConvertFunctionPtr(Call->getCalledValue(), Call), | |
| 385 Call->getNormalDest(), | |
| 386 Call->getUnwindDest(), | |
| 387 Args, "", Inst); | |
| 388 CopyDebug(NewCall, Call); | |
| 389 NewCall->setAttributes(RemoveAttrs(Call->getContext(), | |
| 390 Call->getAttributes())); | |
| 391 NewCall->setCallingConv(Call->getCallingConv()); | |
| 392 NewCall->takeName(Call); | |
| 393 FC->RecordConvertedAndErase(Call, NewCall); | |
| 394 } else if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Inst)) { | |
| 395 Type *ElementTy = Inst->getType()->getPointerElementType(); | |
| 396 Type *ElementTy2 = ArrayType::get(Type::getInt8Ty(Inst->getContext()), | |
| 397 DL->getTypeAllocSize(ElementTy)); | |
| 398 Value *Tmp = CopyDebug(new AllocaInst(ElementTy2, Alloca->getArraySize(), | |
| 399 Alloca->getAlignment(), "", Inst), | |
| 400 Inst); | |
| 401 Tmp->takeName(Alloca); | |
| 402 Value *Alloca2 = new PtrToIntInst(Tmp, IntPtrType, | |
| 403 Tmp->getName() + ".asint", Inst); | |
| 404 FC->RecordConvertedAndErase(Alloca, Alloca2); | |
| 405 } else if (// These atomics only operate on integer pointers, not | |
| 406 // other pointers, so we don't need to recreate the | |
| 407 // instruction. | |
| 408 isa<AtomicCmpXchgInst>(Inst) || | |
| 409 isa<AtomicRMWInst>(Inst) || | |
| 410 // Handle these instructions as a convenience to allow | |
| 411 // the pass to be used in more situations, even though we | |
| 412 // don't expect them in PNaCl's stable ABI. | |
| 413 isa<GetElementPtrInst>(Inst) || | |
| 414 isa<VAArgInst>(Inst) || | |
| 415 isa<IndirectBrInst>(Inst) || | |
| 416 isa<ExtractValueInst>(Inst) || | |
| 417 isa<InsertValueInst>(Inst)) { | |
| 418 FC->ConvertInPlace(Inst); | |
| 419 } | |
| 420 } | |
| 421 | |
| 422 // Convert ptrtoint+inttoptr to a bitcast because it's shorter and | |
| 423 // because some intrinsics work on bitcasts but not on | |
| 424 // ptrtoint+inttoptr, in particular: | |
| 425 // * llvm.lifetime.start/end | |
| 426 // * llvm.eh.typeid.for | |
| 427 static void SimplifyCasts(Instruction *Inst, Type *IntPtrType) { | |
| 428 if (IntToPtrInst *Cast1 = dyn_cast<IntToPtrInst>(Inst)) { | |
| 429 if (PtrToIntInst *Cast2 = dyn_cast<PtrToIntInst>(Cast1->getOperand(0))) { | |
| 430 assert(Cast2->getType() == IntPtrType); | |
| 431 Value *V = Cast2->getPointerOperand(); | |
| 432 if (V->getType() != Cast1->getType()) | |
| 433 V = new BitCastInst(V, Cast1->getType(), V->getName() + ".bc", Cast1); | |
| 434 Cast1->replaceAllUsesWith(V); | |
| 435 if (Cast1->use_empty()) | |
| 436 Cast1->eraseFromParent(); | |
| 437 if (Cast2->use_empty()) | |
| 438 Cast2->eraseFromParent(); | |
| 439 } | |
| 440 } | |
| 441 } | |
| 442 | |
| 443 static void ConvertFunc(DataLayout *DL, FunctionMap *FuncMap, | |
| 444 Function *Func, Function *NewFunc, Type *IntPtrType) { | |
| 445 FunctionConverter FC(IntPtrType, FuncMap); | |
| 446 | |
| 447 // Move the arguments across to the new function. | |
| 448 for (Function::arg_iterator Arg = Func->arg_begin(), E = Func->arg_end(), | |
| 449 NewArg = NewFunc->arg_begin(); | |
| 450 Arg != E; ++Arg, ++NewArg) { | |
| 451 FC.RecordConverted(Arg, NewArg); | |
| 452 NewArg->takeName(Arg); | |
| 453 } | |
| 454 | |
| 455 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); | |
| 456 BB != E; ++BB) { | |
| 457 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
| 458 Iter != E; ) { | |
| 459 ConvertInstruction(DL, IntPtrType, &FC, Iter++); | |
| 460 } | |
| 461 } | |
| 462 FC.Finish(); | |
| 463 | |
| 464 // Remove the ConstantExpr bitcasts we introduced for referencing | |
| 465 // global variables. | |
| 466 FunctionPass *Pass = createExpandConstantExprPass(); | |
| 467 Pass->runOnFunction(*NewFunc); | |
| 468 delete Pass; | |
| 469 | |
| 470 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); | |
| 471 BB != E; ++BB) { | |
| 472 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
| 473 Iter != E; ) { | |
| 474 SimplifyCasts(Iter++, IntPtrType); | |
| 475 } | |
| 476 } | |
| 477 // Cleanup: Remove ptrtoints that were introduced for allocas but not used. | |
| 478 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); | |
| 479 BB != E; ++BB) { | |
| 480 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
| 481 Iter != E; ) { | |
| 482 Instruction *Inst = Iter++; | |
| 483 if (isa<PtrToIntInst>(Inst) && Inst->use_empty()) | |
| 484 Inst->eraseFromParent(); | |
| 485 } | |
| 486 } | |
| 487 } | |
| 488 | |
| 489 char ReplacePtrsWithInts::ID = 0; | |
| 490 INITIALIZE_PASS(ReplacePtrsWithInts, "replace-ptrs-with-ints", | |
| 491 "Convert pointer values to integer values", | |
| 492 false, false) | |
| 493 | |
| 494 bool ReplacePtrsWithInts::runOnModule(Module &M) { | |
| 495 DataLayout DL(&M); | |
| 496 Type *IntPtrType = DL.getIntPtrType(M.getContext()); | |
| 497 FunctionMap FuncMap; | |
| 498 | |
| 499 // To avoid introducing bitcasts for direct function calls, we do | |
| 500 // the conversion in three passes. | |
|
eliben
2013/05/15 22:11:21
Explain what the three passes are/do?
Mark Seaborn
2013/05/16 02:08:40
In the course of adding comments, I realised that
| |
| 501 for (Module::iterator Iter = M.begin(), E = M.end(); Iter != E; ) { | |
| 502 Function *Func = Iter++; | |
| 503 // Intrinsics' types must be left alone. | |
| 504 if (Func->isIntrinsic()) | |
| 505 continue; | |
| 506 | |
| 507 FunctionConverter FC(IntPtrType, NULL); | |
| 508 FunctionType *NFTy = FC.ConvertFuncType(Func->getFunctionType()); | |
| 509 | |
| 510 // In order to change the function's arguments, we have to recreate | |
| 511 // the function. | |
| 512 Function *NewFunc = Function::Create(NFTy, Func->getLinkage()); | |
| 513 NewFunc->copyAttributesFrom(Func); | |
| 514 NewFunc->setAttributes(RemoveAttrs(M.getContext(), | |
| 515 NewFunc->getAttributes())); | |
| 516 Func->getParent()->getFunctionList().insert(Func, NewFunc); | |
| 517 NewFunc->takeName(Func); | |
| 518 NewFunc->getBasicBlockList().splice(NewFunc->begin(), | |
| 519 Func->getBasicBlockList()); | |
| 520 FuncMap[Func] = NewFunc; | |
| 521 } | |
| 522 for (FunctionMap::iterator Iter = FuncMap.begin(), E = FuncMap.end(); | |
| 523 Iter != E; ++Iter) { | |
| 524 ConvertFunc(&DL, &FuncMap, Iter->first, Iter->second, IntPtrType); | |
| 525 } | |
| 526 for (FunctionMap::iterator Iter = FuncMap.begin(), E = FuncMap.end(); | |
| 527 Iter != E; ++Iter) { | |
| 528 // Finally, rewrite references by global variable initializers. | |
| 529 Iter->first->replaceAllUsesWith( | |
| 530 ConstantExpr::getBitCast(Iter->second, Iter->first->getType())); | |
| 531 Iter->first->eraseFromParent(); | |
| 532 } | |
| 533 return true; | |
| 534 } | |
| 535 | |
| 536 ModulePass *llvm::createReplacePtrsWithIntsPass() { | |
| 537 return new ReplacePtrsWithInts(); | |
| 538 } | |
| OLD | NEW |