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